0.3类的创建和引用

最近有点懒,觉得一些基础的语句和格式没必要写,所以这次更新的是类的创建和引用,直接po代码吧,这简洁一点

------------------------------------------------------------------------------------------------------------------------------------------------------------

public class SxtStu {

//属性fields
int id;
String sname;
int age;

Computer comp; //计算机

//方法
void study(){
System.out.println("我在认真学习!!,使用电脑:"+comp.brand);
}

void play(){
System.out.println("我在玩游戏!明日方舟!");
}

//构造方法。用于创建这个类的对象。无参的构造方法可以由系统自动创建。
SxtStu(){
System.out.println("调用了无参的构造方法!");
}

//程序执行的入口,必须要有
//javac Sxtstu.java , java Sxtstu
public static void main(String[] args) {
SxtStu stu = new SxtStu(); //创建一个对象
stu.id=1001;
stu.sname= "lpy";
stu.age = 19;

Computer c1 = new Computer();
c1.brand = "HP";

stu.comp = c1;

stu.play();
stu.study();

}
}

class Computer {
String brand;
}

猜你喜欢

转载自www.cnblogs.com/shit-mountain-filter/p/11240766.html