10,学习抽象与封装

使用类图描述类

image.png

构造方法

能否在创建对象的同时就完成赋值?

class Penguin {
    // 属性
    /* 无参构造方法 */
    public Penguin() {
           name = "qq";
           love = 20;
           sex = "Q仔";
           System.out.println("执行构造方法");
    }
}
image.png

系统提供默认无参构造方法

public Penguin() {

}

系统不再提供默认无参构造方法

public Penguin () {
        name = "qq";
        love = 20;
        sex = "Q仔";
}

public Penguin (String name,int health,int love,String sex ) {
        this.name = name;
        this.health = health;
        this.love = love;
        this.sex = sex;
}

方法重载

image.png

一个例子

class Penguin {
       String name = null; //昵称
       int health = 0; // 健康值
       String sex = null; // 性别
       public void Penguin() {  
               health=10;
               sex="雄";
               System.out.println("执行构造方法");
        }
        public void print() {
               System.out.println("企鹅的名字是" + name + ",健康值是" 
                                                 + health + ",性别是" + sex);
        }
}

Penguin pgn3= new Penguin();
pgn3.print();

运行结果:企鹅的名字是null,健康值是0,性别是null
因为public void Penguin不是构造方法,如果改成public Penguin,就先执行它了。
class Dog {
    private String name = "旺财";   // 昵称
    private int health = 100;  // 健康值
    private int love = 0;     // 亲密度
    public void play(int n) {
        int localv = 0;
        health = health - n;
        System.out.println(name+" "+ localv +" "+health+" "+love);
    }
    public static void main(String[] args) {
        Dog d=new Dog();
        d.play(5);
    }
}
静态方法先调用对象,再调用非静态方法,就可以了。

static静态成员

static特点:
1 随着类的加载而加载,静态会随着类的加载而加载,随着类的消失而消失。说明它的生命周期很长。
2 优先于对象存在。—>静态是先存在,对象是后存在。
3 被所有实例(对象)所共享。
4 可以直接被类名调用

image.png

静态方法只能访问静态属性,不能访问实例属性

一个例子

class Person
{
    public String name;
    public int age;
    static public long  all_count;
    public Person(){
        all_count++;
    }
    public Person( String name , int age ){
        all_count++;
        this.name = name;
        this.age = age;
    }
    // 统计人数的函数
    public long getCount(){
      return all_count;
    }
    // 应该具备找同龄人的功能
    public boolean isSameAge( Person p1 ){
      return this.age == p1.age;
    }
}
class Demo9 
{
    public static void main(String[] args) 
    {
        Person p1 = new Person( "jame" ,  34 );
        Person p2 = new Person( "lucy" ,  34 );

        Person p3 = new Person( "lili" ,  34 );
        Person p4 = new Person();
        System.out.println( p1.getCount() + " " + p2.getCount() + "  " + p3.getCount()  );
        System.out.println( p1.isSameAge( p2 ) );
        System.out.println( p1.isSameAge( p3 ) );
    }
}
运行结果:
4  4  4
true
true

封装

image.png
class Dog {
    private String name = "旺财"; // 昵称
    private int health = 100; // 健康值
    private int love = 0;   // 亲密度
    private String strain = "拉布拉多犬"; // 品种
    public int getHealth() {
        return health;
    }
    public void setHealth (int health) {
        if (health > 100 || health < 0) {
            this.health = 40;
            System.out.println("健康值应该在0和100之间,默认值是40");
        } else
            this.health  =  health;
    }
    // 其它getter/setter方法
}

this


作者:豆约翰
链接:https://www.jianshu.com/p/3bacbcb9bdec
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容