Java from entry to advanced road (XI)

Previous article we introduced about inheritance in Java, then we continue to look at inheritance in Java.

In some cases, we can get through the class inherits the parent class of ways, but there are times when a parent class method provides us with not fully meet our needs, this time we will rewrite the class method as follows:

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Student Student = new new Student ();
 . 4          student.name = "John Doe" ;
 . 5          student.age = 18 is ;
 . 6          student.city = "Beijing " ;
 7          student.userInfo (); // I called Zhang Third, this year 18-year-old 
8  
9          Teacher Teacher = new new Teacher ();
 10          teacher.name =" John Doe " ;
 11          teacher.age = 28 ;
 12         teacher.city = "Shanghai" ;
 13          teacher.userInfo (); // My name is Four, 28 years old, from Shanghai 
14      }
 15  }
 16  
17  class {the Person
 18      String name;
 19      int Age;
 20  
21      void userInfo ( ) {
 22          System.out.println ( "my name is" + name + ", this year's" + age + "years old" );
 23      }
 24-  }
 25  
26  class Student the extends the Person {
 27      String City;
 28  }
 29  
30 class Teacher the extends the Person {
 31      String City;
 32  
33      void userInfo () {
 34          System.out.println ( "My name is" + name + ", this year's" + age + "years old," + "from" + City);
 35      }
 36 }

In the above code, we define the Teacher and Student two columns, and inherit the Person class, except that we define a same parent and the userInfo Person class () method in class Teacher, issued process invention and the same parameters, different methods body, which is actually override class methods.

Rewrite need to follow the "two big two small with the" principle (usually the same):

1, with two

  1) A method with the same name

    1>, void, and when the type of sub-base classes and superclasses must be the same

    2>, when a reference type must be less than or equal to a subclass parent

  2), the same parameter list

2, two small:

  1), the return value of the method of the subclass type parent or less

  2), the method subclass thrown exception than or equal to the parent

3, major

  1), access to the subclass greater than or equal parent

The above example we have actually said "two with", let's look at the type of return value less than or equal to the parent class "two small" in the sub-class method

 1 class Person {
 2     String name;
 3     int age;
 4 
 5     void userInfo() {
 6         System.out.println("我叫" + name + ",今年" + age + "岁");
 7     }
 8 
 9     double num(){ return 1.23; }
10 
11     Person person(){ return null; }
12 
13     Student student(){ return null; }
14 }
15 
16 class Student extends{The Person
 . 17  
18 is      Double NUM () { return 4.56 ;}
 . 19  
20 is  //     int NUM (). 8 {return;} compilation errors, must be substantially the same type 
21 is  
22 is      Student Person () { return  null ;}
 23 is  
24  //     the Person Student () {return null;} compiler error must be less than or equal to the parent reference type 
25 } ·

Method overrides (Override) method makes it easy to think of it we said before overloading (Overload), we'll look at said before overloading.

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Aoo = AOO new new Aoo (); // Create a Aoo objects 
. 4          aoo.foo (); // foo reference no 
. 5          aoo.foo (123 ); // foo integer parameter 123 
. 6          aoo.foo ( "ABC"); // foo string parameter zhangsan 
. 7          aoo.foo ( "ABC", 123); // foo + integer parameter string abc123 
. 8          AOO .foo (123, "ABC"); // foo + int foo string parameter 123abc 
. 9      }
 10  }
. 11  
12 is  class Aoo {
 13 is      void foo () {
 14          System.out.println ( "foo no parameters" );
 15      }
 16  
. 17      void foo ( int NUM) {
 18 is          System.out.println ( "foo integer parameter" + NUM);
 . 19      }
 20 is  
21 is      void foo (string STR) {
 22 is          System.out.println ( "foo string argument" + STR);
 23 is      }
 24  
25      void foo (STR string, int NUM) {
 26 is          the System.out. println ( "foo + integer parameter string" + str +NUM);
 27      }
 28  
29      void foo ( int NUM, String STR) {
 30          System.out.println ( "foo + integer parameter string" NUM + + STR);
 31 is      }
 32 }

Overloading rules:

  • Overloaded method must be changed parameter list (number of parameters or not the same type);
  • Overloaded method may change the return type;
  • The method may be varied overloaded access modifiers;
  • Overloaded methods can declare a new or broader checked exceptions;
  • Or method can be overridden in a subclass in the same class.
  • Type as the return value can not distinguish between a standard function overloading.

The difference between the rewrite and reload

 

Point of difference Overloaded methods Overriding methods
parameter list You must be modified Must not be modified
Return Type You can modify Must not be modified
abnormal You can modify Can be reduced or removed, you must not throw new or broader exceptions
access You can modify Must not be made more stringent restrictions (limit can be reduced)

Method override (Overriding) and heavy (Overloading is the) java polymorphism is different manifestations, a manifestation of polymorphism is rewritten between the parent class and subclass can be understood as polymorphic reload specific performance form.

  • (1) a method overload is defined in the same class names plurality of methods, the same or different types and different order of the number of their arguments, the method is referred to as overloading (Overloading).
  • (2) the name of the method is a method to rewrite the parent class in the same sub-class there is a method, and as the number and type of parameters, the method returns the same value, referred to rewrite (Overriding).
  • (3) a method overload class exhibit polymorphism, and the polymorphism is a method of rewriting performance of the child and parent class.

Overloaded and can use this to rewrite the following diagram to represent the image. Overloaded we can consider a variety of forms, each form of expression can realize their functions. Rewrite is to strengthen its functions based on the original.

 

 

Guess you like

Origin www.cnblogs.com/weijiutao/p/11125113.html