4、类与对象之继承、组合、代理、跳出

面向对象编程的代码复用机制。
继承
继承是OOP中最为重要的概念,达到了非常有效的代码重用效果,使得开发效率变得很高!在Java语言中,两个类之间通过extends关键字实现继承。
class A {   
    public A() {   
        System.out.println("A()!");   
    }   
}   
class B extends A {   
    public B() {   
        System.out.println("B()!");   
    }   
}    
public class ExtendsTest extends B {     
    public ExtendsTest() {   
        System.out.println("ExtendsTest()!");   
    }   
    public static void main(String[] args) {   
        new ExtendsTest();   
    }   
}   
ExtendsTest继承自B,B继承自A,当实例化ExtendsTest的时候,依次打印出A、B、ExtendsTest构造器中的内容,说明:构造器被依次调用了;当子类实现继承时,相当于父类给子类一个实例,故先构造实例,实验中得出结论: 在对子类进行初始化的时候,会先调用父类的构造器,如果父类构造器需要传递参数,则使用super关键字来实现就行了。

1、子类不能继承父类私有的域(属性)或者方法。如果想要继承父类的私有对象,只能将private改成protected,因为protected的权限控制在包内。因此一般情况,用到继承的话,最好将父类中的域声明为私有(private,因为一般情况不需要继承成员变量),将方法声明为public,方便继承。
2、 当调用子类方法,如果子类有,则屏蔽父类方法;如果子类没有,则去调用父类的同名方法,但是调用者保持是子类。

代理
代理的思想在我们讲的设计模式里面有体现,就是在一个类中持有另一个类的实例,从而代替原类进行一个操作,我们看个例子:
public class ProxyTest {   
    Source source = new Source();   
    void p(int n){   
        source.a(n);   
    }   
    void p2(int n){   
        source.b(n);   
    }   
    public static void main(String[] args) {   
        ProxyTest pt = new ProxyTest();   
        pt.p(20);   
        pt.p2(50);   
    }   
}   
class Source{   
    void a(int n){   
        System.out.println("this is : "+n);   
    }   
    void b(int n){   
        System.out.println("this is : "+n);   
    }   
}   
组合
如果大家还记得设计模式里的建造者模式,那么很容易联想到组合机制,就是将一系列的对象组合在一起,组合成一个功能丰富的类,当然,这些对象包括基本数据类型,也包括引用。来看个例子:
class Soap{   
    private String s;   
    Soap(){   
        System.out.println("soap");   
        s = "constructor";   
    }   
    public String toString(){   
        return s;   
    }   
}   
public class CompronentTest {   
    private String s1 = "happy",s2="Happy",s3,s4;   
    private Soap castille;   
    private int i;   
    public CompronentTest(){   
        s3 = "joy";   
        castille = new Soap();   
    }   
    {   
        i = 88;   
    }   
    public String toString(){   
        if(s4 == null){   
            s4 = "Joy";   
        }   
            return "s1 = " + s1 + "\n" +   
                   "s2 = " + s2 + "\n" +   
                   "s3 = " + s3 + "\n" +   
                   "s4 = " + s4 + "\n" +   
                   "i = " + i + "\n" +   
                   "castille = " + castille;   
   
    }   
    public static void main(String[] args) {   
        CompronentTest ct = new CompronentTest();   
        System.out.println(ct);   
    }   
}   

懒加载涉及持有对象实例,所以会涉及到懒加载的机制,代码中的:
if(s4 == null){
         s4 = "Joy";
}
就是一种懒加载的机制,这种机制就是解决当所需的对象比较庞大的时候,只有在用的时候才去初始化,节省空间,提高效率!

Java中跳出循环的方法:
break是跳出当前for循环,switch语句;
break会跳出(终止)当前循环,continue是跳出当前循环,开使下一循环;
以上两种方法没有办法跳出多层循环,如果需要从多层循环跳出,则需要使用标签,定义一个标签label,然后再需要跳出的地方,用break label就行了,代码如下:
public class RecTest {   
    public static void main(String[] args) {   
          loop: for (int i = 0; i < 10; i++) {   
            for (int j = 0; j < 10; j++) {   
                for (int k = 0; k < 10; k++) {   
                    for (int h = 0; h < 10; h++) {   
                        if (h == 6) {   
                            break loop;   
                        }   
                        System.out.print(h);   
                    }   
                }   
            }   
        }   
        System.out.println("\nI'm here!");   
    }   
}   
012345
I'm here!
continue同理;
012345 012345 012345 012345 012345 012345 012345 012345 012345 012345   //h=6时,跳到loop,执行下一个for命令;
I'm here!

猜你喜欢

转载自onway417.iteye.com/blog/2187366