Java Object-oriented-----Super keyword

Source: Internet
Author: User

Java Object-oriented-–super keywords

1: Define father (parent Class) class

1: member variable int x=1;
2: Construction method without parameter and argument, with output statement

2: Define son class extends Father class

1: member variable int y=1;
2: The construction method is non-parametric and has a parameter. have output statement this.y=y+x;

3: Create son class object

Son son=new Son(3);        System.out//4
 class Father {    intx =1; Father () {System.out.println ("This is a parent class without a parameter"); } Father (intx) { This. x = x; System.out.println ("This is a parent class with a reference structure"); }voidSpeak () {System.out.println ("I'm a father."); }                  } class Son extends Father {    inty =1; Son () {System.out.println ("This is a non-parametric construct of a subclass"); } Son (intY) { This. y = y + x; System.out.println ("This is a sub-class with a reference structure"); }voidRun () {Super. speak ();//Accessing functions of the parent classSystem.out.println ("I'm a son."); }} class Demo6 {     Public Static voidMain (string[] args) {Son s =NewSon (3); System.out.println (S.Y);//4}}
4:子类对象为什么可以访问父类的成员。    1:this.y=y+x;有一个隐式的super super.x5:super关键字作用    1:主要存在于子类方法中,用于指向子类对象中父类对象。    2:访问父类的属性    3:访问父类的函数    4:访问父类的构造函数6:super注意

This is like a super, this is a call to the current object, and super points to the parent class of the current calling object. The demo class is loaded, executes the main method, Son.class loads, discovers the parent class Father class, and the Father class is loaded into memory. The class is loaded, the object is created, the constructor of the parent class is called (the default is auto-parameterless), and then the child class is constructed to create a subclass object that also contains a parent class object. The parent class object is inside the subclass object. This super can only be used with objects and cannot be used in static contexts.

2: constructor of subclass default first row default call parent class parameterless constructor, implicit statement super ();
1: The parent class without a parameter constructor does not exist, compile an error.

Son(int y) {        //super();隐式语句        this.y = y + x;        System.out.println("这是子类的有参构造");    }
3:子类显式调用父类构造函数

In the first row of the subclass constructor, the parent class is called with any constructor from the Super keyword. If you explicitly call the parent class constructor, the compiler automatically adds a constructor that calls the parent class without parameters to disappear. Calls between constructors can only be placed on the first line and can be called only once. Super ()
And this () cannot exist at the same time as the first row of the constructor.

Son(int y) {        super(y);// 子类显式调用父类构造函数        this.y = y + x;        System.out.println("这是子类的有参构造");    }Son(int y) {        this();  //不能同时存在构造函数第一行          super(y);        this.y = y + x;        System.out.println("这是子类的有参构造");    }
4:super思考

If a developer customizes a class and does not display the inheritance of classes, can the member functions in that class use Super Guanjianjian words? You can use, inherit from the object class, which is the parent class of all classes.

class Demo7 {    public  voidprint(){        System.out.println(super.toString());    }    publicstaticvoidmain(String[] args){        new Demo7().print();        System.out.println();        }}
5:继承练习

7: Overwrite (override)

1: Defining the Father Class

1: Name, meal method, eat can窝窝头pickles.
2: Define son class, inherit father

The 1:son class does not define any members, the subclass creates the object, and the method of eating can still be called.
2: Father's way of eating, son does not want to eat. Son himself defined the method of eating.

1: There is a way to eat in the parent class, there are 2 ways to eat in the subclass, exactly the same, but the method body is not the same.
2: Two functions in a class are identical and are not allowed.

1: Compile run, execute the method of subclass.
2: Using the parent class method, in the subclass method, use Super. The parent class method name.

 class Father {String name;voidEat () {System.out.println ("Eat litters"); }} class Son extends Father {     Public voidEat () {//Inheritance can enable subclasses to enhance the parent class's methodsSystem.out.println ("a couple of dishes."); System.out.println ("two cups."); System.out.println ("A hot drink."); System.out.println ("Take one."); }} class Demo8 {     Public Static voidMain (string[] args) {Son s =NewSon ();//methods for executing subclassesS.eat (); }}

3: The phenomenon is called rewriting (overwrite override)

1: In inheritance, a subclass can define a function that has the same name as the parent class and a consistent argument list, and this function
Called a function rewrite.

4: Premise

1: Must have an inheritance relationship

5: Features

1: When a subclass overrides a function of the parent class, the object of the child class must call the function if it is called.
You can make a call to an overriding function of the parent class by using the Super keyword.
2: Inheritance can make subclasses enhance methods of parent class

6: Details

1: Function names must be the same
2: Parameter list must be the same
3: Subclass overrides the function of the parent class, the function must have access permission greater than or equal to the parent class's function or compile an error
4: When a subclass overrides a function of the parent class, the return value type must be the return value type of the parent class or the subclass of the return value type. You cannot return a data type that is larger than the parent class: such as the subclass function return value type is Object

    1:定义 A B  C 类 B extends A      2:Father类中定义A getA();    3:Son 类中重写getA(); 方法,尝试将返回值修改为B,C ,Object        1:B编译通过        2:C 编译失败 ,没有继承关系        3:Object编译失败,比父类的返回值类型更大
 class A {} class B extends A {} class C {} class Father {String name;voidEat () {System.out.println ("Eat litters"); }//define a function to get the object of Class A,A Geta () {return NewA (); }} class Son extends Father {     Public voidEat () {//Inheritance can enable subclasses to enhance the parent class's methodsSystem.out.println ("two cups."); System.out.println ("a couple of dishes.");Super. Eat (); System.out.println ("Take one."); }//Class B is a subclass of Class AB Geta () {return NewB (); }} class Demo8 {     Public Static voidMain (string[] args) {Son s =NewSon ();    S.eat (); }}

7: The order in which a subclass object looks for a property or method:

Principle: the nearest principle.
If the object of the subclass calls the method, the default is to use this to find, and if the current object does not find a property or method, find the object that the Super keyword maintains in the current object, and if no compile error has been found, locate the direct call.

8: Different overloads and overrides

1: Heavy Load (overload):

1: Prerequisite: All overloaded functions must be in the same class
2: Feature: function name is the same, parameter list is different, not related to other (access control character, return value type)
3: Different: Different numbers, different order, different types

2: Rewrite (override):

1: Premise: Inheritance
2: Features:
Function names must be the same, parameter lists must be the same.
The return value type of the subclass is equal to or less than the return value of the parent class

9: Rewriting exercises

Describe different kinds of animals.
1: Defining Animal classes
There's a name, there's a way to eat and bark
2: Definition of dog inheritance animal rewriting the method of eating and barking of the parent class
3: Definition of cat inherit animals rewrite the way the parent eats and barks

 class Animal{    intx=1; String name;voidEat () {System.out.println ("eat."); }voidShout () {System.out.println ("I'm an animal."); }} class Dog extends Animal{    voidEat () {System.out.println ("Chew Bones"); }voidShout () {System.out.println ("Wang Wang"); }voidEat (String food) {System.out.println ("Eat:"+food); }} class Cat extends Animal{    voidEat () {System.out.println ("Eat rats."); }voidShout () {System.out.println ("Meow Meow"); }} class Demo9{     Public Static voidMain (string[] args) {Dog d=NewDog ();        D.shout ();        D.eat (); Cat c=NewCat ();        C.shout ();        C.eat ();        System.out.println (); }}

"is looking at my blog of the children's shoes, I see you imposing, the conversation between the faint king of the gas, there will be a future!" There is a "top" word, you can conveniently point it (to login csdn account first) "

-willing to share and progress together!
- More articles please see: Http://blog.csdn.net/duruiqi_fx

Java Object-oriented-----Super keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.