Java Learning Note (iii): Object-oriented

Source: Internet
Author: User

1. Inheritance
    • Grammar

      class 父类{}class 子类 extends 父类{}
      Only multilayer inheritance, not multiple inheritance, subclasses cannot directly access private members in the parent class, can be obtained by getter and setter methods
    • Child class object new instantiation (when)
    1. The constructor of the parent class is called by default, and then the constructor of the subclass is called to instantiate so that if the parent class and the subclass constructor contain output statements, they will be printed at new
    2. In fact, super () is implied in the subclass construct, and the parent class construct is called
    • Override of Method ("method overrides")
    1. The subclass override method cannot have more restrictive access than the parent class method
      (such as: Parent class--public, subclass must be--public)
    2. Parent method How to access the parent class method after the quilt class method is overwritten?
      ```
    3. Use super (); keywords can be, such as: Super.print ();
    4. In subclasses, you can access methods in the parent class directly from the Super. Method () Form
      ```
    5. The exception:private--> to default is not to be written, but to redefine a new method!
    • Override of property
    1. Redefine attributes after inheritance, consistent with overwrite methods
    2. Special: In subclasses :

      1.super.name-->访问父类中的name属性2.this.name-->访问“本“类中的name属性
    • The difference between overloading and overwrite
    1. Overload:

      01.方法名相同参数类型或个数不同02.对权限没有要求03.发生在同一个类中
    2. Overwrite:

      01.方法名称,参数类型相同02.被覆写的方法(即子类方法)不能拥有(比父类方法)更严格的访问权限03.发生在继承类中
    • The role of the Super keyword
    1. Super calls the parent class construct

      **同this一样,该语句必须放在构造方法的句首**
    2. The difference between this and super:

This

01.属性:调用本类中的属性,若没有,则从父类中寻找02.方法:调用本类中的方法,若没有,则从父类中寻找03.构造:调用本领构造,必须放在构造方法的首行04.特殊:表示当前的对象

Super

01.属性:访问父类中的属性02.方法:直接访问父类中的方法03.构造:调用父类构造,必须放在子类的构造方法里的首行,如:super();
    • Final keyword

Attention:

1. 使用final声明的“类‘’不能有子类2. 使用final声明的”方法”不能被子类所覆写3. 使用final声明的变量即为常量,常量不可修改!4. public static final同时声明的变量称为全局”常“量
2. Abstract class
    • Defining rules
      ```
    1. A class that contains an abstract method must be an abstract class
    2. Both abstract and abstract methods use the abstract keyword declaration
    3. Abstract methods only need to be declared without implementing
    4. Abstract classes must be quilt-inherited, and subclasses (if not abstract classes) must overwrite all abstract methods in the abstract class
    5. Abstract classes cannot be instantiated directly, and subclasses must be instantiated through non-abstract subclasses
      ```
    • Defining formats
      ```
    1. Abstract class: Abstracted class A {}
    2. Abstract method: Access Permissions Abstract return value type method name (parameter);
      There is no method body in an abstract method, and you do not need {}
    3. Subclass Definition Method: Class B extends a{}
      The subclass must also overwrite all abstract methods in the abstract class
    4. Note: Abstract methods should not be declared with the private keyword, because abstract methods must be overridden by the quilt class
    5. Essence: Abstract analogy common class more abstract method, in addition to the object can not be directly instantiated operation is actually not different
      ```

      3. Interface
    • Composition
    1. One of the most important concepts
    2. consists of global constants and common abstract methods

      interface 接口名称 {全局常量;抽象方法;}
    3. The abstract method here must be defined as public access rights, cannot be changed!!!
    4. Of course, you can not write, default public,
    5. Generally, we know that the global constants are public static final, the abstract method is public abstract, but here special, default so can omit the definition, actually there are

interface A{    String s="作者"; //定义全局常量    void print(); //定义抽象方法    String getInfo(); //抽象方法不需{}}
    • Use of interfaces
    1. Class subclass name implements interface A, interface b{} !!!
      A subclass can implement multiple interfaces,Class A implements b,c{}
    2. After implementation, all abstract methods must be covered
    • Inheritance of interfaces
    1. An interface cannot inherit an abstract class, but an interface allows multiple interfaces to be inherited

      interface A{} interface B{} interface C extends B,C{}
    2. That is, an interface can inherit multiple interfaces at the same time, note that it inherits extends!!!
    3. Interfaces cannot implement interfaces and can implement inheritance

      4. The polymorphism of the object
    • Two kinds of
    1. Overloading and overriding of methods
    2. The polymorphism of the object
      01. Transition Up: Child Object----Parent class object, automatically completed
      02. Transition Down: Parent Object--child class object, must specify the subclass type of transformation
    • Format
    1. Object Transformation: Parent Class object = Subclass instance;

      如:B b =new B(); //子类实例化过程  A a =b; //向上转型
    2. Object down: Subclass sub-Class object = (subclass) Parent class instance;

      如:A a =new B(); //向上转型B b =(B)a; //向下转型:A-->B
    • Call: call the subclass's own method, and must only instantiate the object with the child class
    • NOTE:!! Before you proceed to a downward transformation of an object, you must first change the object's upward transformation, or the exception
    • READ: a program to go from right to left:

      如:A a =new B();是B实例化了一个对象,该B的对象再向上转型成为了A的对象a
      5.instanceof keywords
    • An instance of which class is used to determine exactly who an object is.
    1. Object instanceof Class--Returns a Boolean value
    • Use:
    1. As follows:

      A a1=new B(); A a2=new A();
      01.A1 objects that are both A and B--objects instantiated by subclasses are also instances of the parent class and subclass, so you can transform directly up or down
      02.A2 is just the object of a and cannot be turned down at this time because it is no longer a subclass instance, so it cannot be transformed without first being transformed--using the parent class to instantiate the class object.
    2. Make judgments before you move down to avoid anomalies
      ```
      A a=new B ();
      if (a instanceof B) {}

Java Learning Note (iii): Object-oriented

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.