学习路线--Java SE

Chapter10 Java封装

2018-03-12  本文已影响0人  Mr董先森

Lecture1 封装的实现

Tips:只有getXXX方法的属性是只读属性;只有setXXX方法的属性是只写属性

package Chapter10.Lecture1.cat;

/**
 * 宠物猫类的封装
 */
public class Cat {
    //1.修改属性的可见性--private,限定只能在当前类内被访问
    private String name;    //昵称
    private int month;      //年龄

    public Cat(){

    }

    //通过带参构造方法赋值
    public Cat(int month) {
        this.setMonth(month);
    }

    //2. 创建getter/setter方法
    public String getName() {
        return "我是一只叫" + name + "的宠物猫";
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        if (month <= 0) {
            System.out.println("信息输入错误,年龄必须大于0");
        } else {
            this.month = month;
        }
    }
}
package Chapter10.Lecture1.cat;

public class CatTest {
    public static void main(String[] args) {
        //对象实例化
        Cat one = new Cat();
        //通过带参构造方法创建类
        Cat two = new Cat(-2);
        //测试
        //会输出属性month的初始值0
        System.out.println("年龄:"+two.getMonth());

        one.setName("花花");
        System.out.println(one.getName());

        one.setMonth(-1);
        if (one.getMonth() == 0) {
            return;
        }
        System.out.println("年龄:" + one.getMonth());
    }
}

建议采用import 包名.类名;的方式加载,提高效率

Tips:加载类的顺序跟import导入语句的位置无关

java.lang 包含Java语言的基础的类,该包系统加载时默认导入,如:System、String、Math
java.util 包含Java语言中常用工具,如:Scanner、Random
java.io 包含输入、输出相关功能的类,如File、InputStream

Lecture2 static关键字

推荐调用方式:类名.静态成员

package Chapter10.Lecture2.animal;

public class Cat {
    //静态成员、类成员
    public static int price;   //售价

    private String name;    //昵称
    private int month;  //年龄
    private double weight;  //体重
    private String species; //品种

    {
        System.out.println("我是构造代码块");
    }

    static {
        System.out.println("我是静态代码块");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public static void eat() {
        //静态方法中不能直接访问同一个类中的非静态成员,只能直接调用同一个类中的静态成员
        //run();
        //name = "小明";
        //只能通过对象实例化后,对象.成员方法的方式访问非静态成员
        Cat temp = new Cat();
        temp.run();
        temp.name = "小明";
        //静态方法中不能使用this
        //this.name = "小明";
        Cat.price = 100;
        System.out.println("小猫吃鱼");
    }

    //在成员方法中,可以直接访问类中的静态成员
    public void run() {
        {
            System.out.println("我是普通代码块");
        }
        this.name = "凡凡";
        Cat.price = 20;
        eat();//此行代码会导致栈溢出,注释掉后执行(原因:递归调用)
        System.out.println("售价是" + Cat.price + "的" + this.name + "快跑");
    }
}
package Chapter10.Lecture2.test;

import Chapter10.Lecture2.animal.Cat;

public class Test {
    public static void main(String[] args) {
        Cat one = new Cat();
        one.setName("花花");
        one.setMonth(2);
        one.setSpecies("英国短毛猫");
        //one.price = 2000;
        Cat.price = 2000;

        Cat two = new Cat();
        two.setName("凡凡");
        two.setMonth(1);
        two.setSpecies("中华田园猫");
        //two.price = 150;
        Cat.price = 150;

        System.out.println(one.getName() + "的售价为:" + one.price);
        System.out.println(two.getName() + "的售价为:" + two.price);

        //one.eat();
        Cat.eat();
        System.out.println("----------------------");

        one.run();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读