2017-03-16-向上转型
Xplorist Lv6

2017-03-16-向上转型

知识点1:

子类中的属性和父类中的属性同名:子类中存在父类中的属性,但是无法调用父类的属性。

重写:子类对父类的方法进行重新定义,保持方法、参数列表、返回值类型不变,只改变方法体。重写的方法不能比父类中的方法有更严格的权限,父类中的方法的权限是private,子类不能对其进行重写。

知识点2:

final

final修饰的变量是常量

final修饰的方法不能被子类重写

final修饰的类不能被继承

final修饰的引用类型只是它的引用类型变量中存的只是堆中分配的空间的地址不能改变。

只要把内存分析弄懂,其实很多东西不用死记。

知识点3:

向上转型:子类对象指向父类的引用

例如:class Dog extends Animal(){}

Animal a=new Dog();

子类中拥有所有父类的属性和方法,所以子类堆空间的地址可以指向父类引用,父类的引用只能够调用子类对象中与父类相同的部分。

知识点4:

Object类和toString()方法;

Test t=new Test();

在System.out.println(t);中系统会自动调用Object的toString()方法。然后将t对象的类的路径和对象的哈希地址找出来。

可以通过重写toString()方法来实现改变输出。

实例1:

    /*
     * 1.给猫喂食喂小鱼干,给狗喂食喂狗饼干
     * 2.与猫一起玩,玩毛球,与狗一起玩,狗接飞盘
     * 3.新增显示猫狗信息的方法【名字、饥饿值、友好度】
     * */

核心代码:

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
public static void main(String[] args) {
        test2();
    }

    
    
    public static void test2(){
        // 创建狗对象
        Dog dog = new Dog("萨摩耶", "白色");
        // 创建猫对象
        Cat cat = new Cat("豹猫", "杂色");
        
        // 创建一个宠物店对象
        PetShop ps = new PetShop(cat,dog);
        ps.show();
        
        // 创建一个主人对象
        Master master = new Master();
        
        // 主人到宠物店里买宠物
        master.buyPet(ps);
        
        boolean flag=true;
        while(flag){
            Scanner sc=new Scanner(System.in);
            System.out.println("请选择您要做得事:1、喂食,2、玩耍:(乱选就退出)");
            String input=sc.next();
            switch (input) {
            case "1":
                master.feed();
                break;
            case "2":
                master.play();
                break;
            default:
                flag=false;
                System.out.println("正在退出····");
                System.out.println("已经退出了");
                break;
            }
        }
    }


public class Master {
    private String name;
    private Pet pet;
    
    public Master() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的名字:");
        name=sc.next();
    }

    public Master(String name) {
        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(PetShop ps){
        ps.sellPet(this);
    }
    
    public void feed(){
        System.out.print(name+"喂"+pet.getType());
        String food="";
        if(pet instanceof Cat){
            food="小鱼干";
            System.out.println("小鱼干");
            
        }else{
            food="狗饼干";
            System.out.println("狗饼干");
        }
        pet.eat(food);
        System.out.println(pet);
    }
    
    public void play(){
        System.out.print(name+"和"+pet.getType());
        if(pet instanceof Cat){
            System.out.println("玩毛球");
        }else{
            System.out.println("玩接飞盘");
        }
        pet.play(this);
        System.out.println(pet);
    }
    
}

public class PetShop {
    private Cat cat;
    private Dog dog;

    public PetShop(Cat cat, Dog dog) {
        this.cat = cat;
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void show() {
        System.out.println("宠物店里面有:【"+ cat.getColor()+"的"+ cat.getType() +  "】和【" + dog.getColor()+"的"+ dog.getType()+"】" );
    }

    public void sellPet(Master master) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择要买的动物:1、猫,2、狗(乱输入则默认为狗)");
        String s=sc.next();
        
        switch (s) {
        case "1":
            master.setPet(cat);
            System.out.println("【"+master.getName()+"】买了"+cat.getType());
            break;
        case "2":
            master.setPet(dog);
            System.out.println("【"+master.getName()+"】买了"+dog.getType());
            break;

        default:
            master.setPet(dog);
            System.out.println("【"+master.getName()+"】买了"+dog.getType());
            break;
        }
        
    }
}

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

    }

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

    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 String getName() {
        return name;
    }

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

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

    public void eat(String food) {
        if(hunger>0){
            this.hunger--;
            System.out.println(type + "吃"+food+"后,饥饿度减少1");
        }else{
            System.out.println("【"+type+"已经吃饱了,不要再喂了,喂了它也不会吃了,要不你试试再喂几次看看他的饥饿值有没有变。】");
        }
    }
    
    public void play(Master master){
        if(hunger>5){
            System.out.println("请给【"+type+"】喂食,太饿了,不能够继续玩耍,当饥饿值小于等于5才能玩耍");
        }else{
            this.favor++;
            this.hunger++;
            System.out.println(master.getName()+"与"+this.type+"玩耍之后,好感度增加了1,饥饿度增加了1");
        }
    }
    
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "名字:"+type+" 饥饿值:"+hunger+" 友好度:"+favor;
    }

}
 评论