类和对象说明
类的属性和方法
类的入门案例
//类名
public class school {
//属性
String name; //名称
int jsNumber; //教室数目
int jfNumber;//机房数目
//方法
public void show(){
System.out.println("名称: "+ name + "教室数目" + jsNumber + " , 机房数目:" + jfNumber);
}
}
public class Demo01 {
public static void main (String[] args){
//声明
School school;
//分配空间
school = new School();
//点访问属性
school.name = "华夏高中";
school.jsNumber = 36;
school.JFNumber = 8;
school.show();
}
}
类的入门案例内存图
public class Demo01 {
public static void main (String[] args){
//声明
//School school;
School school = new School();
//点访问方法
school.show(); //名称:null,教室数目:0,机房数目:0
//点访问属性
school.name = "华夏高中";
school.jsNumber = 81;
school.JFNumber = 9;
school.show();
}
}
游客案例
public static void main(Stringl] args)]{
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入姓名:");
String name = scanner.next();
if (name.equals("n")) break;
// 提供了一个退出通道
System.out.print("请输入年龄:");
int age = scanner.nextInt();
Visitor visitor = new Visitor();
// 创建对象
visitor.name = name :
visitor.age = age;
visitor.show();
}
System.out.println("退出程序");
}