public Student(){};
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public void study(){
System.out.println("我叫"+name+"我今年"+age);
}
}
class DemoPhone {
public static void main(String[] args) {
Phone p=new Phone("三星",4800);
Phone p1=new Phone();
p1.setName("三星");
System.out.println(p.getName());//不要忘了p.
}
}
class Phone{
private String name;
private int price;
public Phone(){}
public Phone(String name,int price){
this.name=name;
this.price=price;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name; //也可以写return this.name;
}
public void call(){
System.out.println("打电话");
}
public void sendMessage(){
System.out.println("发信息");
}
public void playGame(){
System.out.println("玩游戏");
}
}
class JuXing{
public static void main(String[] args) {
Ju j=new Ju(3,5);
j.zhouchang();
j.area();
}
}
class Ju {
private int width;
private int high;
public Ju(){}
public Ju(int width,int high){
this.width=width;
this.high=high;
}
public void setWidth(int width){
this.width=width;
}
public int getWidth(){
return width;
}
public void setHigh(int high){
this.high=high ;
}
public int getHigh(){
return high;
}
public void zhouchang(){
int z=2*(width+high);
System.out.println(z);
}
public void area(){
int a=width*high;
System.out.println(a);
}
}
4:一个类的成员变量初始化过程
Student s = new Student();
首先是1.主函数的class文件加载进方法区,2主函数入栈3.Student类的class文件入方法区,4栈中创建一个对象的引用,
5堆中创建了一个对象,6.给对象进行默认初始化7.对属性进行显示赋值8.构造方法入栈,对对象属性进行赋值然后构造方法弹栈
9把地址值赋给对象的引用