Java语言关键字
Java关键字是电脑语言里事先定义的,有特别意义的标识符,有时又叫保留字,还有特别意义的变量。
Java标识符
标识符(identifier)是指用来标识某个实体的一个符号,在不同的应用环境下有不同的含义。
语法规则
1.标识符由字母、数字、下划线“_”、汉字、美元符号“$”组成,第一个字符不能是数字。
2.不能把java关键字和保留字作为标识符。
3.标识符没有长度限制。
4.标识符对大小写敏感。
练习:创建Teacher类和Student类,分别位于不同的包,并将Teacher类属性设置为Public,在Student类调用属性,进行介绍
package pack1;
public class Teacher {
public String name;
public int age;
public Teacher(String name,int age){
this.name=name;
this.age=age;
}
}
package pack2;
import pack1.Teacher;
public class Student {
String name;
int age;
char gender;
double weight;
boolean isMarry;
Teacher teacher;
public Student(){}
public Student(String name,int age,char gender,double weight,boolean isMarry,Teacher teacher){
this.name=name;
this.age=age;
this.gender=gender;
this.weight=weight;
this.isMarry=isMarry;
this.teacher=teacher;
}
public void jieShao(){
System.out.println("大家好,我叫"+name+",我今年"+age+"岁,"+"性别:"+gender+",体重:"+weight+",婚否:"+isMarry+"。"+"我的导师是"+teacher.name+"老师,他今年"+teacher.age+"岁。");
}
public static void main(String[] args){
java.util.Scanner scanner=new java.util.Scanner(System.in);
System.out.print("请输入学生姓名:");
String name=scanner.next();
System.out.print("请输入学生年龄:");
int age =scanner.nextInt();
System.out.print("请输入性别:");
char gender=scanner.next().charAt(0);
System.out.print("请输入体重:");
double weight=scanner.nextDouble();
System.out.print("请输入是否已婚(true/false):");
boolean isMarry=scanner.nextBoolean();
Teacher teacherDu=new Teacher("老杜",88);
Student studentJi=new Student(name,age,gender,weight,isMarry,teacherDu);
studentJi.jieShao();
}
}