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

  《JAVA编程思想》学习备忘(第237页:Reusing Classes)-3
Name hiding
If a Java base class has a method name that's overloaded several times,redefining that method name in the derived class will not hide any of the base-class versions.Thus overloading works regardless of whether the method was defined at this level or in a base class:
//: reusing/Hide.java
// Overloading a base-class method name in a derived
// class does not hide the base-class versions.
import static net.mindview.util.Print.*;
class Homer{
    char doh(char c){
        print("doh(char)");
        return 'd';
    }
    float doh(float f){
        print("doh(float)");
        return 1.0f;
    }
}
 
class Milhouse{}
 
class Bart extends Homer{
    void doh(Milhouse m){
        print("doh(Milhouse)");
    }
}
 
public class Hide{
    public static void main(String[] args){
        Bart b = new Bart();
        b.doh(1);
        b.doh('x');
        b.doh(1.0f);
        b.doh(new Milhouse());
    }
}
输出结果:
doh(float)
doh(char)
doh(float)
doh(Milhouse)
 
Java SE5 has added the @Override annotation,which is not a keyword but can be used as if it were.When you mean to override a method,you can choose to add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding:
//:reusing/Lisa.java
//{CompileTimeError}(Won't compile)
class Lisa extends Homer{
    @Override void doh(Milhouse m){
        System.out.println("doh(Milhouse)");
    }
}
The{CompileTimeError}tag excludes the file from this book's Ant build,but if you compile it by hand you'll see the error message:
method does not override a method from its superclass
The @Override annotation will thus prevent you from accidentally overloading when you don't mean to.
 
Choosing composition vs. inheritance
Both composition and inheritance allow you to place subojects inside your new class(composition explicitly does this-with inheritance it's implicit).You might wonder about the difference between the two,and when to choose one over the other.
Composition is generally used when you want the functinality of an existing class inside your new class,but not its interface.That is ,you embed an object so that you can use it to implement features in your new class,but the user of your new class sees the interface you've defined for the new class rather than the interface from the embedded object.For this effect,you embed private objects of existing classes inside your new class.
Sometimes it makes sense to allow the class user to directly access the composition of your new class;that is,to make the member objects public.The member objects use implementation hiding themselves,so this is a safe thing to do.When the user knows you're assembling a bunck of parts,it makes the interface easier to understand.A car object is a good example:
//:reusing/Car.java
// Composition with public objects.
class Engine{
    public void start(){}
    public void rev(){}
    public void stop9){}
}
class Wheel{
    public void inflate(int psi){}
}
class window{
    public void rollup(){}
    public void roolldown(){}
}
class Door(){
    public Window window = new Window();
    public void open(){}
    public void close(){}
}
public class Car{
    public Engine engine = new Engine();
    public Wheel[] wheel = new Wheel[4];
    public Door
      left = new Door(),
      right = new Door();//2-door
    public Car(){
        for(int i = 0; i < 4; i++)
            wheel[i] = new Wheel();
    }
    public static void main(String[] args){
        Car car = new Car();
        car.left.window.rollup();
        car.wheel[0].inflate(72);
    }
}
Because in this case the composition of a car is part of the analysis of the problem(and not simply part of the underlying design),making the members public assists the client programmer's understanding of how to use the class and requires less code complexity for the creator of the class.However,keep in mind that this is a special case,and that in general you should make fields private.
When you inherit,you take an existing class and make a special version of it.In general,this means that you're taking a general-purpose class and specializing it for a particular need.With a little thought,you'll see that it would make no sense to compose a car using a vehicle object-a car doesn't contain a vehicle,it is a vehicle.The is-a relationship is expressed with inheritance,and the has-a relationship is expressed with composition
(待续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值