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

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專(zhuān)輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

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

?? Java技術(shù)收集很多各種技術(shù)總結(jié)
?? TXT
字號(hào):
作者: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; }
}


?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲国产成人高清精品| 日韩**一区毛片| 日韩欧美卡一卡二| av在线综合网| 免费一级欧美片在线观看| 日本一二三不卡| 777亚洲妇女| 一道本成人在线| 国产精品亚洲人在线观看| 日韩国产精品久久久久久亚洲| 欧美激情一区在线| 精品久久久久久久一区二区蜜臀| 欧美制服丝袜第一页| 国产在线播放一区二区三区| 日本中文字幕不卡| 亚洲午夜久久久久久久久久久| 久久一日本道色综合| 欧美日韩国产中文| 一本一道久久a久久精品| 国产乱码精品一区二区三区忘忧草| 日韩经典一区二区| 一区二区激情视频| 国产目拍亚洲精品99久久精品| 欧美日韩国产一二三| 色猫猫国产区一区二在线视频| 国产成人亚洲精品青草天美 | 午夜精品成人在线| 1024成人网| 国产精品日韩成人| 亚洲免费成人av| 国产精品成人免费精品自在线观看| 精品久久人人做人人爱| 91麻豆精品国产自产在线观看一区 | 午夜a成v人精品| 亚洲综合一区在线| 亚洲影视资源网| 一区二区免费在线播放| 亚洲综合一区二区精品导航| 亚洲免费资源在线播放| 中文字幕亚洲成人| 亚洲欧洲日产国产综合网| 国产精品另类一区| 亚洲国产精品激情在线观看| 中文字幕精品—区二区四季| 久久久国产一区二区三区四区小说| 亚洲精品在线免费播放| 精品久久一区二区三区| 久久亚洲一区二区三区四区| 精品免费99久久| 久久久亚洲午夜电影| 久久免费电影网| 欧美精彩视频一区二区三区| 国产免费成人在线视频| 国产精品入口麻豆原神| 亚洲欧美色综合| 亚洲国产一区二区在线播放| 无吗不卡中文字幕| 奇米影视在线99精品| 国产尤物一区二区在线| 国产91清纯白嫩初高中在线观看| 不卡av免费在线观看| eeuss影院一区二区三区| 99国内精品久久| 日本久久一区二区| 91.麻豆视频| 欧美一级高清片在线观看| 久久品道一品道久久精品| 国产精品久久久久久福利一牛影视 | 亚洲综合视频网| 婷婷综合另类小说色区| 激情另类小说区图片区视频区| 国产精品99久久久久久有的能看| 99久久综合狠狠综合久久| 欧美羞羞免费网站| 精品精品欲导航| 中文字幕日韩av资源站| 日日夜夜一区二区| 国产盗摄女厕一区二区三区| 在线亚洲+欧美+日本专区| 精品三级在线看| 亚洲视频一二区| 五月激情六月综合| av激情成人网| 日韩欧美综合在线| 精品国产乱码91久久久久久网站| 国产精品视频一二| 视频一区二区欧美| 国产大陆a不卡| 欧美酷刑日本凌虐凌虐| 国产清纯白嫩初高生在线观看91| 一区二区三区不卡视频在线观看| 日韩国产在线观看一区| 国产一区二区精品在线观看| 欧美午夜精品久久久久久超碰| 久久亚洲精品国产精品紫薇| 中文字幕一区二区三区在线不卡 | 日韩精品专区在线影院重磅| 1024成人网色www| 老司机一区二区| 91美女片黄在线观看91美女| 精品久久国产字幕高潮| 玉米视频成人免费看| 国产一区二区三区av电影| 在线观看亚洲专区| 国产欧美一二三区| 青青国产91久久久久久| 99精品在线免费| 久久久久久一二三区| 日韩国产精品久久久久久亚洲| 99久久精品一区二区| 日韩欧美一区在线| 一区二区三区在线观看欧美| 国产成人亚洲综合a∨婷婷图片| 制服丝袜一区二区三区| 日韩毛片一二三区| 国产美女在线观看一区| 欧美肥妇bbw| 亚洲国产乱码最新视频| 成人av电影在线| 国产午夜精品久久久久久久| 美女被吸乳得到大胸91| 欧美绝品在线观看成人午夜影视 | 不卡的av电影在线观看| 久久这里只有精品首页| 日本女人一区二区三区| 欧美三级蜜桃2在线观看| 亚洲婷婷在线视频| 国产超碰在线一区| 在线成人av网站| 亚洲综合一区二区| 在线亚洲一区观看| 自拍偷拍亚洲综合| 色香蕉久久蜜桃| 亚洲精选免费视频| 在线精品观看国产| 一区二区三区免费网站| 日本久久电影网| 亚洲自拍偷拍网站| 欧美日韩三级一区二区| 亚洲电影在线免费观看| 欧美日韩免费观看一区三区| 亚洲成人三级小说| 正在播放亚洲一区| 麻豆一区二区在线| 久久一区二区三区国产精品| 国产一二精品视频| 亚洲欧洲精品成人久久奇米网| 成人美女视频在线看| 亚洲人午夜精品天堂一二香蕉| 色综合久久综合中文综合网| 亚洲国产日韩a在线播放性色| 欧美性色综合网| 日韩国产精品91| 26uuu色噜噜精品一区| 成人一区二区三区视频在线观看| 国产精品久久久久久久裸模| 91视频精品在这里| 亚洲二区视频在线| 日韩一区二区三区免费看| 韩国三级中文字幕hd久久精品| 久久久精品国产免费观看同学| 国产电影精品久久禁18| 亚洲欧洲精品天堂一级| 欧美精品久久一区二区三区| 伦理电影国产精品| 国产精品伦一区二区三级视频| 色狠狠一区二区| 日本伊人色综合网| 亚洲国产精品成人综合色在线婷婷| 99久精品国产| 日本三级亚洲精品| 欧美激情在线看| 欧美色欧美亚洲另类二区| 久久er99热精品一区二区| 中文字幕免费不卡在线| 欧美在线观看视频一区二区 | 国产成人av一区二区| 亚洲日本va午夜在线电影| 精品视频一区二区不卡| 久久电影网站中文字幕| 中文字幕亚洲欧美在线不卡| 欧美一区二区三区小说| 从欧美一区二区三区| 午夜成人免费电影| 中文字幕不卡一区| 91麻豆精品国产91久久久更新时间| 国产在线视频不卡二| 一区二区三区丝袜| 日韩区在线观看| 91国模大尺度私拍在线视频| 国产专区综合网| 一区二区三区高清不卡| www成人在线观看| 欧美唯美清纯偷拍| 粉嫩高潮美女一区二区三区| 免费成人在线网站| 亚洲伦理在线精品| 国产午夜精品在线观看| 91精品国产乱码久久蜜臀| bt7086福利一区国产|