《JAVA编程思想》学习备忘(第237页:Reusing Classes)-5

《JAVA编程思想》学习备忘(第237页:Reusing Classes)-4
protected
Now that you've been introduced to inheritance,the keyword protected finally has meaning,In an ideal world,the private keyword would be enough.In real projects,there are times when you want to make something hidden from the world at large and yet allow access for members of derived classes.
子类可以访问到:
The protected keyword is a nod to pragmatism.It says,"This is private as far as the class user is concerned,but available to anyone who inherits from this class or anyone else in the same package."(protected also provides package access.)
Although it's possible to create protected fields,the best approach is to leave the fields private;you should always preserve your right to change the underlying implementation.You can then allow controlled access to inheritors of your class through protected methods:
//:reusing/Orc.java
//The protected keyword.
import static net.mindview.util.Print.*;
class Villain{
    private String name;
    protected void set(String nm){ name = nm; }
    public Villain(String name){ this.name = name; }
    public String toString(){
        return "I'm a Villain and my name is " + name;
    }
}
public class Orc extends Villain{
    private int orcNumber;
    public Orc(String name, int orcNumber){
        super(name);
        this.orcNumber = orcNumber;
    }
    public void change(String name,int orcNumber){
        set(name); // Available because it's protected
        this.orcNumber = orcNumber;
    }
    public String toString(){
        return "Orc " + orcNumber + ": " + super.toString();
    }
    public static void main(String[] args){
        Orc orc = new Orc("Limburger", 12);
        print(orc);
        orc.change("Bob",19);
        print(orc);
    }
}
输出结果:
Orc 12: I'm a Villain and my name is Limburger
Orc 19: I'm a Villain and my name is Bob
You can see that change() has access to set() because it's protected.Also note the way Orc's toString() method is defined in terms of the baseclass version of toString().
 
Upcasting
inheritance guarantees that all of the methods in the base class are also available in the derived class,any message you can send to the base class can also be sent to the derived class:
//:reusing/Wind.java
// Inheritance & upcasting.
class Instrument{
    public void play(){}
    static void tune(Instrument i){
        i.play();
    }
}
//Wind objects are instruments
//because they have the same interface:
public class Wind extends Instrument{
    public static void main(String[] args){
        Wind flute = new Wind();
        Instrument.tune(flute); //Upcasting
    }
}
...the act of converting a Wind reference into an Instrument reference is called upcasting.
Why "upcasting"?
...Upcasting is always safe because you're going from a more specific type to a more general type.That is, the derived class is a superset of the base class.It might contain more methods than the base class,but it must contain at least the methods in the base class.The only thing that can occur to the class interface during the upcast is that it can lose methods,not gain them.This is why the compiler allows upcasting without any explicit casts or other special notation.
 
Composition vs. inheritance revisited
In object-oriented programming,the most likely way that you'll create and use code is by simply packaging data and methods together into a class,and using objects of that class.You'll also use existing classes to build new classes with composition.Less freguently,you'll use inheritance.So although inheritance gets a lot of emphasis when teaching OOP,it doesn't mean that you should use it everywhere you possibly can.On the contrary,you should use it sparingly,only when it's clear that inheritance is useful.
(待续)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值