"Code Note" Java Basics: Java methods and classes

Source: Internet
Author: User

  1. Java as an object-oriented language with three major features (object-oriented core): Encapsulation inheritance polymorphism
    1. Encapsulation: It's packing, everything is packed.
    2. Inheritance: Subclasses can inherit from parent class
      • File name: Student.java
      •  1  public  class       Student { 2  public   String name;  3  public  int   age;  4   Public  void   study () { 6  System.out.println ("Student Learning"  7   8  }
          • The common method Student (Student) is defined in the above and defines a common behavior-general class learning (study)
          • Student as a constructor, constructors cannot be inherited, so there is no constructor override
      • File name: Unstudent.java
      • 1  Public classUnstudentextendsstudent{2      Public voidStudy () {3System.out.println ("Learning Methods for college students");4     }5      Public voidPlayGame () {6SYSTEM.OUT.PRINTLN ("College students play DotA");7     }8}
          • In the above, the general method of college students (unstudent) was defined and inherited (extends) General method students (Study)
          • At the same time, it inherits the premise of the parent class (Study) in the subclass (Unstudent), which defines a method that is the same as the parent class (method name, parameter, return type) ("Public void Study () {
            ”)。
          • Subclasses can define a new behavior (Class) "PlayGame".
      • File name: Test.java
      • 1  Public classTest {2      Public Static voidMain (string[] args) {3         //Automatic Transformation4Unstudent US =Newunstudent ();5Student US1 =Newunstudent ();6Student US2 =NewStudent ();7         8 us.study ();9 us1.study ();Ten us2.study (); One          A us.playgame (); - us1.playgame (); - us2.playgame (); the     } -}
          • In the main function above, the university student (unstudent) "Us" is constructed once, and the method is overloaded. "Us" will have the common method unstudent all the attributes, and the method overrides will overwrite the definition of the parent class's original class.
          • "US1" has all student properties, but the property that is overridden by unstudent will be the contents of the Unstudent rewrite, the original student within the class will be overwritten, unstudent exists but student does not exist in the class "US1" will not be called.
          • "US2" is the authentic student class.
      1. There are two ways of doing this:
        • Automatic transformation
          Definition: Create the object of the subclass and then automatically transform to the type of the parent class
          Format: The parent class class name Object name = new Subclass class name ();
        • The transformed object type is the transformed type, the method that executes if the subclass overrides the method of the parent class, then executes the override, and if there is no override, it executes the parent class's own
          If there is no automatic transformation process, then the method of execution is your own method
    3. Because of features such as automatic transformation and method rewriting and inheritance, the Java language presents polymorphism.
      1. Multiple objects of the same type, executing the same method, performing a different process (there are differences)
      2. Why is there a polymorphic phenomenon???
        • Automatic Transformation + method rewriting
        • Auto-Transformation + method overrides will result in inheritance and will therefore be polymorphic
  2. There are only three ways to classify the Java language:
    1. Construction method: public class name () {}
    2. Common method: Public return type method name (parameter) {}
    3. Abstract method: Public abstract return type method name (parameter);
  3. There are also three categories of Java language classes:
    1. Normal class: public class Name {}
      1. There can only be ordinary methods and construction methods, there must be no abstract method
      2. Can create objects
      3. The property can be a constant (static final), or it can be a variable
    2. Abstract class: Public abstract class Name {}
      1. There are three types of methods that can exist
      2. Cannot create object
      3. The property can be a constant (static final), or it can be a variable
    3. Interface: Public interface class name () {}
      1. There can only be abstract methods, common methods and construction methods do not exist
      2. Cannot create object
      3. property must be constant (static final)
  4. What is the use of interfaces and abstract classes?
    1. The role of interfaces and abstract classes is used when the parent class is inherited, and the abstract method is used to instruct the subclass to rewrite (implement) the
    2. Key Sub: Implements
    3. Format:
      • public class subclass class name implements interface name {
        Overriding all the abstract methods of the parent class
        }
    4. Interface is nothing more than a leader, but a guide to development
  5. File name: Teacher.java
    1  Public InterfaceTeacher {2      Public Static FinalString name = "AA";3      Public Static FinalString job = "AA";4      Public Static Final intAge = 20;5     6     //Abstract Methods7      Public Abstract voidplay1 ();8      Public Abstract voidplay2 ();9      Public Abstract voidplay3 ();Ten      Public Abstract voidPlay4 (); One      Public Abstract voidplay5 (); A}
  6. File name: Unteacehr.java
    1  Public Abstract class Implements teacher{2     3 }

      1. In the above two files, the "Teacher" class as the interface Class (keyword interface represents the interface class
      2. has three constants and cannot be changed.
      3. There are abstract methods that cannot be implemented and defined.

"Code Note" Java Basics: Java methods and classes

Related Article

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.