今天跟着马士兵老师的视频学了一天的面向对象的思想,说实话,有点头昏脑涨哈哈。
马士兵老师重点讲了内存的占用和释放。
栈内存和堆内存:
栈内存会为形式参数、局部变量(成员变量)、临时变量分配存储空间,当方法结束的时候,栈内存会释放。
堆内存会为对象开辟存储空间,对象将被复制给引用变量,当堆上的对象没有被引用变量所引用的时候,将会被垃圾回收机制在某种时刻回收。
还有方法的重构和方法的重写,继承之中的构造方法。今天受益匪浅,要回去好好消化一下。
package test3;
public class Person {
String name;
int id;
double weight;
public static int sid;
public Person(String name,double weight) {
this.name = name;
id = sid++;
this.weight = weight;
}
public void printf() {
System.out.println(name+"\nid="+id+"\nweight="+weight);
}
public static void main(String[] args) {
Person p1 = new Person("xiaoming",29.0);
System.out.println(p1.id);
Person p2 = new Person("xiaoli",25.0);
System.out.println(p2.weight +" "+ p2.id);
p1.printf();
p2.printf();
Teacher t1 = new Teacher("laoshi",95.0,"qinghuadaxue");
t1.printf();
}
}
class Teacher extends Person{
String school;
public Teacher(String name,double weight,String school) {
super(name,weight);
this.school = school;
}
public void printf() {
super.printf();
System.out.println("school:"+school);
}
}
class Text{
public static void main(String[] args) {
Person p1 = new Person("xiaoming",29.0);
System.out.println(p1.id);
Person p2 = new Person("xiaoli",25.0);
System.out.println(p2.weight +" "+ p2.id);
p1.printf();
p2.printf();
Teacher t1 = new Teacher("laoshi",95.0,"qinghuadaxue");
t1.printf();
}
}