2017-03-15-封装,继承,super,静态块和代码块
Xplorist Lv6

2017-03-15-封装,继承,super,静态块和代码块

知识点1:

静态块:在类加载就开始执行,并且只执行一次,作用:用于初始化类的静态成员变量

static {

}

代码块:在创建对象的时候就加载一次,再次创建对象则再次加载,作用:用于在创建对象之前,比构造方法还优先执行,初始化一些类的成员变量

{

}

知识点2:

权限:

private                同类中       

default                同类中      同包中

protected            同类中      同包中       子类中

public                  同类中      同包中       子类中       任何地方

记忆:不用记忆private 和public ,因为一个最小权限,一个最大权限,需要注意的就是default和protected两个权限的范围

记忆的窍门:只要记住default是包权限就行了,protected的权限比default的大就OK了。

protected:

父类的protected的属性在子类中可以使用,但是不能在静态方法(包括main方法)中使用。 

知识点3:

封装:

将类中的所有的属性都设置为私有的权限,然后提供get和set方法分别用来取属性和设置属性。

知识点4:

继承:

子类继承父类后,享有父类的所有属性和方法。

this和super:

this指代当前对象

super指代父类对象

实例1:

/*
     * 1.
     * 有一个宠物店的类,类里面有猫和狗两种宠物,
     * 宠物店有一个方法可以查看猫狗的属性
     * 2.
     * 新增主人Master类,主人可以去商店购买宠物
     * 3.
     * 新增主人给宠物喂食的功能【新增宠物吃方法、主人新增喂食方法】
     * 4.
     * 新增主人和宠物玩耍的功能
     * 5.
     * 给宠物新增属性好感度,每次玩耍后好感度增加,新增饥饿值,每次喂食后饥饿值降低
     * 6.
     * 当饥饿值达到一定值之后,便不能玩耍
     * */

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
public static void main(String[] args) {
        //创建狗对象
        Dog dog=new Dog("萨摩耶","白色");
        //创建猫对象
        Cat cat=new Cat("豹猫", "杂色");
        
        //创建一个宠物店对象
        PetShop ps1=new PetShop(dog);//向上转型:子类对象可以自动转父类,宠物店里面反正有宠物,你想卖什么宠物你自己装什么类型的宠物,设置猫宠物就是猫,设置狗宠物就是狗
        ps1.show();
        
        //创建一个宠物店对象
        PetShop ps2=new PetShop(cat);//向上转型
        ps2.show();
        
        //创建一个主人对象
        Master master=new Master("张三");
        
        //主人分别买狗和猫
        master.buyPet(dog);
        master.buyPet(cat);
        
        //创建食物对象,主人将食物喂给狗
        Food dogFood=new Food("狗粮");
        master.feed(dog, dogFood);
        
        //创建食物对象,主人将食物喂给猫
        Food catFood=new Food("猫粮");
        master.feed(cat, catFood);
        
        //主人拿着猫粮去和猫玩耍(拿猫粮的原因:防止猫饿了不和主人玩)
        master.play(cat,catFood);
        
        //主人拿着狗粮去和狗玩耍(拿狗粮的原因:防止狗饿了不和主人玩)
        master.play(dog,dogFood);
    }
    
}

package com.share.demo03_15;

public class Pet {
    private String type;
    private String color;
    private int favor;
    private int hunger;

    public Pet() {

    }

    public Pet(String type, String color) {
        this.type = type;
        this.color = color;
        this.favor=0;
        this.hunger=10;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getFavor() {
        return favor;
    }

    public void setFavor(int favor) {
        this.favor = favor;
    }

    public int getHunger() {
        return hunger;
    }

    public void setHunger(int hunger) {
        this.hunger = hunger;
    }

    public void show() {
        System.out.println("类型:" + this.getType());
        System.out.println("颜色:" + this.getColor());
    }

    public void eat(Food food) {
        hunger--;
        System.out.println(type + "吃" + food.getName());
    }
    
    public void play(Master master){
        if(hunger>5){
            System.out.println(type+"太饿了,不能够继续玩耍");
        }else{
            this.favor++;
            System.out.println(master.getName()+"与"+this.type+"的好感度增加了1");
        }
    }
    
    public void play(Food food,Master master){
        while(hunger>5){
            System.out.println(type+"当前饥饿值为:"+hunger+"大于5,太饿了,不能够继续玩耍");
            eat(food);
        }
        play(master);
    }

}

package com.share.demo03_15;

public class Master {
    private String name;
    private Pet pet;
    
    public Master() {
        super();
    }

    public Master(String name) {
        super();
        this.name = name;
    }

    public Pet getPet() {
        return pet;
    }

    public String getName() {
        return name;
    }

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

    public void setPet(Pet pet) {
        this.pet = pet;
    }
    
    public void buyPet(Pet pet){
        this.pet=pet;
    }
    
    public void feed(Pet pet,Food food){
        System.out.println(name+"喂"+pet.getType());
        pet.eat(food);
    }
    
    public void play(Pet pet,Food food){
        pet.play(food,this);
    }
    
}

 评论