笔记two:面向对象的思想(1)

今天跟着马士兵老师的视频学了一天的面向对象的思想,说实话,有点头昏脑涨哈哈。
马士兵老师重点讲了内存的占用和释放。
栈内存和堆内存:
栈内存会为形式参数、局部变量(成员变量)、临时变量分配存储空间,当方法结束的时候,栈内存会释放。
堆内存会为对象开辟存储空间,对象将被复制给引用变量,当堆上的对象没有被引用变量所引用的时候,将会被垃圾回收机制在某种时刻回收。
还有方法的重构和方法的重写,继承之中的构造方法。今天受益匪浅,要回去好好消化一下。

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值