类、属性、方法详解
/**
* 类、属性、方法
* 编写一个学生类
*/
public class SxtStu {
//一个class是一个类 类名SxtStu
int id;
int age;
String name;
//id age name为属性field 是成员变量 随对象创建、消失
//属性不赋值会变成默认值 int默认0 String默认null char默认\u0000 boolean默认false
public void study(){
//方法study 动态方法不用静态修饰符static
System.out.println("学习中");
}
public void playFootball(){
System.out.println("踢球射门");
}
//上面为创建了一个类class 类里面有多个属性field 类里面还包含一些方法
//在平台或者命令行里java SxtStu 的时候,首先加载类的属性和方法,然后通过main方法进入程序
//加载的类的信息进入方法区method area 信息被分为动态方法(study/playFootball)、常量("学习中""踢球射门"等固定不变的量)、static的属性和方法(main)
public static void main(String[] args) {
//main方法是面向过程 程序的入口
//加载完类以后 系统开始调用main方法 在栈stack里生成main()的栈帧 一个方法是一帧
//main方法的参数(args)没有赋值所以默认为null
SxtStu s1 = new SxtStu();
//用SxtStu类生成一个新的new对象 赋地址给s1 s1为地址
//s1地址指向的对象拥有了类的属性id age name
//s1指向的对象被生成在堆heep中 对象中包含属性 并且可以调用类的方法
System.out.println(s1.id);
System.out.println(s1.name);
//打印s1地址指向的对象的属性,没有赋值输出默认值
s1.study();
s1.playFootball();
//位于栈stack里的main栈帧里的s1 的地址指向的位于堆heep中的对象 调用方法区method area里面类SxtStu的方法 学习study和踢球playFootball
s1.id = 1001;
//变更s1指向的对象的属性id 变更为1001
}
}

