package jy201701;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author PeicongHe
*
*/
public class Practice {
/**
* 输入并输出一串字符串
*/
public static void test1(){
int a = 0;
try {
while((a=System.in.read())!=-1){
//read(byte[])
// -1并非便是当输入为-1时跳出循环,而表示的是字符串的末尾
System.out.print((char)a);//转化为字符
if ((char)a=='q'){
System.exit(0);//表示当输入q时,程序会跳出循环
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 由于read()是一个一个字符来读取的,读取速度会很慢
* 用字符串定义读取的长度
*/
public static void test2(){
byte myArray[] = new byte[10];
try{
System.in.read(myArray);
}catch(IOException e){
e.printStackTrace();
}
System.out.println(myArray);//直接输出会出现乱码
System.out.println(new String(myArray));//输出的长度是10
}
/**
* 使用console()时,编译执行文件要在命令行上执行
* 首先找到文件的地址,打开所在的文件夹
* 然后对文件进行编译 例如:javac Practice.java
* 然后后退到bin目录下,可进行执行。例如:java Practice
*/
public static void test3(){
String lineStr = System.console().readLine();
System.out.println("要在命令行执行");
System.out.println(lineStr);
}
/**
* 利用nextLine进行行输出
*/
public static void test4(){
Scanner s = new Scanner(System.in);//扫描并存储在s
while(s.hasNextLine()){//hasNextLine 判断是否存在下一行,如果存在为true,不存在为fault
System.out.println(s.nextLine());//输出当前行的内容
}
}
/**
* System.consofe().readPassword()
* 命令行输入密码
*/
public static void test5(){
if(System.console()!=null){//输入不为空,就输入密码
char password[] = System.console().readPassword();//readPassword(char[]) 输入类型是字符,所以在存储密码的时候要用到字符串存储
System.out.println(new String(password));//输出的时候可以用转化为String类型输出字符串
}else{
System.err.println("请不要在Eclipse中执行");
}
}
public static void main(String[] args){
test5();
}
}
package jy201701;
/**
* 继承
* @author PeicongHe
*
*/
class Animal{ //定义一个父类
String name; //定义一个属性
void shout(){ //定义一个方法
System.out.println("会吠");
}
}
class Dog extends Animal{
//子类(dog)继承父类(Animal)
void printName(){ //方法
System.out.println("name="+name);
}
void shout(){
System.out.println("汪汪..."); //重写父类
}
}
public class Inherit {
public static void main(String[] args){
Dog dog = new Dog(); //创建一个dog的对象
dog.name="中华田园犬"; //为dog类的name属性进行赋值
dog.printName(); //调用Dog类的printName()方法
dog.shout(); //调用Dog类继承来的shout()方法
}
}
/**
* 1、子类只能拥有一个父类
* 2、父类可以拥有多个子类
* 3、可以多层继承
*/