亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? overloading overriding runtime type and object orientation (1).txt

?? 這是一本很好的Java開發(fā)書籍
?? TXT
字號:
作者:jeru
email: jeru@163.net
日期:7/3/2001 10:01:58 AM
6)Overloading overriding runtime type and object orientation
Objective 1)
State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a".

Encapsulation involves hiding data of a class and allowing access only through a public interface. 

The separation of interface and implementation makes it easier to modify the code within a class without breaking any other code that uses it.

encapsulation, inheritance, polymorphism are three principal characteristics of Object Oriented programming.

Implementing OO relationships
·    “is a” relationship is implemented by inheritance (extends keyword)
·    “has a” relationship is implemented by providing the class with member variables.

Overloading and Overriding
·    Overloading is an example of polymorphism. (operational / parametric)
·    Overriding is an example of runtime polymorphism (inclusive)
·    A method can have the same name as another method in the same class, provided it forms either a valid overload or override


Objective 2) 
Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods.

Overloading    Overriding
Signature has to be different. Just a difference in return type is not enough.    Signature has to be the same. (including the return type)
Accessibility may vary freely.    Overriding methods cannot be more private than the overridden methods.
Exception list may vary freely.    Overriding methods may not throw more checked exceptions than the overridden methods.  But can throw no exception.
Just the name is reused. Methods are independent methods. Resolved at compile-time based on method signature.    Related directly to sub-classing. Overrides the parent class method. Resolved at run-time based on type of the object.
Can call each other by providing appropriate argument list.    Overriding method can call overridden method by super.methodName(), this can be used only to access the immediate super-class’s method. super.super won’t work. Also, a class outside the inheritance hierarchy can’t use this technique.
Methods can be static or non-static. Since the methods are independent, it doesn’t matter. But if two methods have the same signature, declaring one as static and another as non-static does not provide a valid overload. It’s a compile time error.    static methods don’t participate in overriding, since they are resolved at compile time based on the type of reference variable. A static method in a sub-class can’t use ‘super’ (for the same reason that it can’t use ‘this’ for)Remember that a static method can’t be overridden to be non-static and a non-static method can’t be overridden to be static. In other words, a static method and a non-static method cannot have the same name and signature (if signatures are different, it would have formed a valid overload)
There’s no limit on number of overloaded methods a class can have.    Each parent class method may be overridden at most once in any sub-class. (That is, you cannot have two identical methods in the same class)


·    Variables can also be overridden, it’s known as shadowing or hiding. But, member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since it’s already resolved at compile time based on the reference variable type. Only methods are resolved at run-time.

public class Shadow {
  public static void main(String s[]) {
    S1 s1 = new S1();
    S2 s2 = new S2();
    
    System.out.println(s1.s); // prints S1
    System.out.println(s1.getS()); // prints S1
    System.out.println(s2.s); // prints S2
    System.out.println(s2.getS()); // prints S2

    s1 = s2;
    System.out.println(s1.s); // prints S1, not S2 - 
                      // since variable is resolved at compile time
    System.out.println(s1.getS()); // prints S2 - 
                     // since method is resolved at run time    
  }
}

class S1 {
  public String s = "S1";
  public String getS() {
    return s;
  }
}

class S2 extends S1{
  public String s = "S2";
  public String getS() {
    return s;
  }
}
In the above code, if we didn’t have the overriding getS() method in the sub-class and if we call the method from sub-class reference variable, the method will return only the super-class member variable value. For explanation, see the following point.

·    Also, methods access variables only in context of the class of the object they belong to. If a sub-class method calls explicitly a super class method, the super class method always will access the super-class variable. Super class methods will not access the shadowing variables declared in subclasses because they don’t know about them. (When an object is created, instances of all its super-classes are also created.) But the method accessed will be again subject to dynamic lookup. It is always decided at runtime which implementation is called. (Only static methods are resolved at compile-time)

public class Shadow2 {
  String s = "main";
  public static void main(String s[]) {
    S2 s2 = new S2();
    s2.display();  // Produces an output – S1, S2

    S1 s1 = new S1();
    System.out.println(s1.getS()); // prints S1
    System.out.println(s2.getS()); // prints S1 – since super-class method always 
     // accesses super-class variable

  }
}

class S1 {
  String s = "S1";
  public String getS() {
    return s;
  }
  void display() {
    System.out.println(s);
  }
}

class S2 extends S1{
  String s = "S2";
  void display() {
    super.display();   // Prints S1
    System.out.println(s); // prints S2
  }
}

·    With OO languages, the class of the object may not be known at compile-time (by virtue of inheritance). JVM from the start is designed to support OO. So, the JVM insures that the method called will be from the real class of the object (not with the variable type declared). This is accomplished by virtual method invocation (late binding). Compiler will form the argument list and produce one method invocation instruction – its job is over. The job of identifying and calling the proper target code is performed by JVM.
·    JVM knows about the variable’s real type at any time since when it allocates memory for an object, it also marks the type with it. Objects always know ‘who they are’. This is the basis of instanceof operator.
·    Sub-classes can use super keyword to access the shadowed variables in super-classes. This technique allows for accessing only the immediate super-class. super.super is not valid. But casting the ‘this’ reference to classes up above the hierarchy will do the trick. By this way, variables in super-classes above any level can be accessed from a sub-class, since variables are resolved at compile time, when we cast the ‘this’ reference to a super-super-class, the compiler binds the super-super-class variable. But this technique is not possible with methods since methods are resolved always at runtime, and the method gets called depends on the type of object, not the type of reference variable. So it is not at all possible to access a method in a super-super-class from a subclass.

public class ShadowTest {
    public static void main(String s[]){
        new STChild().demo();
    }
}
class STGrandParent {
    double wealth = 50000.00;
    public double getWealth() {
        System.out.println("GrandParent-" + wealth);
        return wealth;
    }
}
class STParent extends STGrandParent {
    double wealth = 100000.00;
    public double getWealth() {
        System.out.println("Parent-" + wealth);
        return wealth;
    }
}
class STChild extends STParent {
    double wealth = 200000.00;

    public double getWealth() {
        System.out.println("Child-" + wealth);
        return wealth;
    }

    public void demo() {
        getWealth(); // Calls Child method
        super.getWealth(); // Calls Parent method
        //super.super.getWealth(); // Compiler error, GrandParent method cannot be accessed
        ((STParent)this).getWealth(); // Calls Child method, due to dynamic method lookup
    ((STGrandParent)this).getWealth(); // Calls Child method, due to dynamic method
      // lookup

        System.out.println(wealth); // Prints Child wealth
        System.out.println(super.wealth); // Prints Parent wealth
        System.out.println(((STParent)(this)).wealth); // Prints Parent wealth
        System.out.println(((STGrandParent)(this)).wealth); // Prints GrandParent wealth
    }
}

·    An inherited method, which was not abstract on the super-class, can be declared abstract in a sub-class (thereby making the sub-class abstract). There is no restriction. In the same token, a subclass can be declared abstract regardless of whether the super-class was abstract or not.
·    Private members are not inherited, but they do exist in the sub-classes. Since the private methods are not inherited, they cannot be overridden. A method in a subclass with the same signature as a private method in the super-class is essentially a new method, independent from super-class, since the private method in the super-class is not visible in the sub-class.

public class PrivateTest {
    public static void main(String s[]){
        new PTSuper().hi();     // Prints always Super
        new PTSub().hi();     // Prints Super when subclass doesn't have hi method
                 // Prints Sub when subclass has hi method
        PTSuper sup;
        sup = new PTSub();
        sup.hi();     // Prints Super when subclass doesn't have hi method
            // Prints Sub when subclass has hi method
    }
}

class PTSuper {
    public void hi() { // Super-class implementation always calls superclass hello
        hello();
    }
    private void hello() {     // This method is not inherited by subclasses, but exists in them.
            // Commenting out both the methods in the subclass show this.
            // The test will then print "hello-Super" for all three calls
            // i.e. Always the super-class implementations are called
        System.out.println("hello-Super");
    }
}

class PTSub extends PTSuper {
    public void hi() { // This method overrides super-class hi, calls subclass hello
        try {
            hello(); 
        }
        catch(Exception e) {}

    }

    void hello() throws Exception {     // This method is independent from super-class hello
                // Evident from, it's allowed to throw Exception
        System.out.println("hello-Sub");
    }
}

·    Private methods are not overridden, so calls to private methods are resolved at compile time and not subject to dynamic method lookup. See the following example.

public class Poly {
    public static void main(String args[]) {
        PolyA ref1 = new PolyC();
        PolyB ref2 = (PolyB)ref1;

        System.out.println(ref2.g());     // This prints 1
                    // If f() is not private in PolyB, then prints 2
    }
}

class PolyA {
    private int f() { return 0; }
    public int g() { return 3; }
}

class PolyB extends PolyA {
    private int f() { return 1; }
    public int g() { return f(); }
}

class PolyC extends PolyB {
    public int f() { return 2; }
}


?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩欧美一区二区免费| 不卡av免费在线观看| 亚洲欧美色图小说| 日韩免费电影网站| 欧美三片在线视频观看| 91性感美女视频| 亚洲电影一级片| 亚洲一区中文在线| 日韩精品国产精品| 久久国产精品色| 国产不卡在线视频| 97久久超碰国产精品| 欧美色图片你懂的| 日韩精品久久久久久| 亚洲啪啪综合av一区二区三区| 色综合天天综合网国产成人综合天| 国产一区二区三区日韩| 青青国产91久久久久久| 五月婷婷激情综合网| 亚洲成人精品在线观看| 亚洲午夜久久久久久久久电影网 | 黄色资源网久久资源365| 午夜久久福利影院| 另类小说一区二区三区| 久久国产精品第一页| 国产麻豆精品久久一二三| 久久97超碰色| 国产91精品一区二区麻豆网站 | 国产精品久久三| 中文字幕在线一区二区三区| 中文字幕在线播放不卡一区| 亚洲男人天堂av网| 天天av天天翘天天综合网| 老司机午夜精品| 99热精品一区二区| 欧美福利视频一区| 欧美高清在线一区二区| 亚洲色图20p| 久久99久久精品欧美| av激情亚洲男人天堂| 欧美理论电影在线| 国产日产欧产精品推荐色| 中文字幕一区三区| 美女视频免费一区| 91香蕉视频黄| 亚洲精品在线观看视频| 亚洲国产成人精品视频| 国产一区二区三区不卡在线观看| 国产福利一区二区三区视频 | 欧美精品一区二区三区蜜臀| 综合分类小说区另类春色亚洲小说欧美| 亚洲午夜视频在线| 国产精品亚洲专一区二区三区 | 欧美日韩一区二区三区在线| 精品99久久久久久| 首页亚洲欧美制服丝腿| 成人深夜视频在线观看| 欧美大尺度电影在线| 亚洲综合激情小说| 国产成人综合亚洲91猫咪| 欧美三级欧美一级| 亚洲男人天堂一区| 色av综合在线| 亚洲人成电影网站色mp4| 国产精品中文字幕一区二区三区| 欧美午夜精品一区二区三区 | 日韩福利视频网| 国内外成人在线| 精品国产乱码久久久久久夜甘婷婷| 亚洲综合一区二区三区| jlzzjlzz欧美大全| 中文字幕国产精品一区二区| 麻豆精品视频在线观看视频| 在线成人免费观看| 亚洲国产一区视频| 欧美日韩一区在线观看| 日韩中文字幕不卡| 欧美一区二区三区男人的天堂| 一区二区免费在线| 91国偷自产一区二区三区观看| 国产精品久久久久影院色老大| 国产精品亚洲а∨天堂免在线| 欧美不卡一区二区三区| 久久精品av麻豆的观看方式| 日韩精品在线一区二区| 日韩中文字幕一区二区三区| 日韩欧美亚洲国产另类| 国产成人日日夜夜| 亚洲色图欧洲色图婷婷| 欧美片网站yy| 国产伦精品一区二区三区视频青涩| 国产色爱av资源综合区| 日本高清无吗v一区| 奇米影视在线99精品| 日本一区二区不卡视频| 91蜜桃在线观看| 日韩精品亚洲一区| 中日韩av电影| 欧美色爱综合网| 日本一不卡视频| 亚洲欧美自拍偷拍色图| 欧美理论在线播放| 懂色av一区二区在线播放| 国产精品免费看片| 欧美一区午夜视频在线观看| av电影一区二区| 日韩av中文字幕一区二区三区| 精品视频在线看| 97国产一区二区| 青青草一区二区三区| 欧美videos大乳护士334| 一本一道综合狠狠老| 国产揄拍国内精品对白| 亚洲精品一区二区三区精华液| 午夜精品爽啪视频| 欧美电影免费观看高清完整版在线| 亚洲一区二区三区视频在线播放| 99久久婷婷国产| 亚洲综合在线五月| 日韩三级免费观看| 亚洲最新视频在线播放| 久久精品国产久精国产| 日本道免费精品一区二区三区| 久久久久国产精品麻豆| 中文字幕不卡在线| 丁香六月综合激情| 国产九九视频一区二区三区| 手机精品视频在线观看| 精品电影一区二区三区| 丁香六月久久综合狠狠色| 秋霞成人午夜伦在线观看| 中文无字幕一区二区三区 | 91免费观看视频在线| 一区二区在线观看视频在线观看| 欧美日韩精品一区视频| 99久久婷婷国产综合精品| 午夜a成v人精品| 亚洲高清在线视频| 久久久99免费| 亚洲欧美日韩综合aⅴ视频| 久久久精品免费免费| 欧美国产视频在线| 中文字幕高清一区| 国产亚洲综合性久久久影院| 精品福利二区三区| 亚洲精品美腿丝袜| 亚洲国产视频在线| 99精品视频一区二区| 91在线国产福利| 欧美三级电影网站| 欧美大片日本大片免费观看| 久久综合狠狠综合久久综合88| 国产精品美女久久久久久2018| 一区二区三区电影在线播| 中文字幕一区二区三区av| 亚洲另类春色校园小说| 热久久一区二区| 粉嫩久久99精品久久久久久夜| 色哟哟在线观看一区二区三区| 欧美日韩黄色一区二区| 久久精品亚洲一区二区三区浴池 | 欧美日韩视频在线观看一区二区三区 | 久久精品免费观看| 99久久久国产精品| 欧洲一区二区三区免费视频| 精品国产亚洲在线| 亚洲成人在线免费| 久久国产精品露脸对白| 在线播放一区二区三区| 自拍偷拍亚洲综合| 国产麻豆午夜三级精品| 日韩一级大片在线观看| 一区二区国产盗摄色噜噜| 狠狠色丁香婷婷综合| 91超碰这里只有精品国产| 亚洲伊人色欲综合网| 91视视频在线观看入口直接观看www | 欧美日韩成人综合天天影院| 日韩美女啊v在线免费观看| 成人精品视频一区二区三区| 2023国产精品自拍| 美国一区二区三区在线播放| 在线成人小视频| 蓝色福利精品导航| 精品久久久久久亚洲综合网| 国产精品麻豆视频| 色女孩综合影院| 一区二区成人在线| 一本久道中文字幕精品亚洲嫩| 中日韩av电影| 日本韩国欧美一区二区三区| 亚洲精品久久久蜜桃| gogogo免费视频观看亚洲一| 国产精品国模大尺度视频| jizzjizzjizz欧美| 一区二区三区自拍| 日韩一区二区三区免费观看| 99精品国产91久久久久久 | 欧美日韩精品一区二区三区蜜桃 | 日韩av中文字幕一区二区|