"Code Note" Java Basics: Inheritance of classes (constructors)

Source: Internet
Author: User

    • In Java, the format of the created object is:

Class Name Object name = new class name ();

Such as:

New JFrame ();
    •  
    1. Constructing objects
    2. Initialize Properties
  • One of our common Java formats:
    • public  return type   method name (parameter) {}
    • Such as:
1  Public void Study () {}      
    • This is called a " method " in Java.
    • The so-called method, which is used to solve a class of problems, is an orderly combination of code, is a functional module.

    • The "constructor" in Java, commonly referred to in the C language as "constructor/constructor method".
    • Its definition format:
      • public class name () {}

  • File name:Teacher.java
  • 1  Public classTeacher {2 String name;3     4     //constructs a method that executes when the object is constructed5      PublicTeacher () {6SYSTEM.OUT.PRINTLN ("The construction method of the teacher class" was performed.));7     }8      PublicTeacher (String N) {9Name =N;Ten     } One      A     //Method Overloading -      Public voidTeachinta) { -SYSTEM.OUT.PRINTLN ("Executive teaching Method 1"); the     } -      Public voidteach () { -SYSTEM.OUT.PRINTLN ("Executive teaching Method 2"); -     } +      Public voidteach (String N) { -SYSTEM.OUT.PRINTLN ("Executive teaching Method 3"); +     } A      Public voidTeach (String N,inta) { atSYSTEM.OUT.PRINTLN ("Executive teaching Method 4"); -     } -      Public voidTeachinta,string N) { -SYSTEM.OUT.PRINTLN ("Executive teaching Method 2"); -     } -}

  • File name:Test.java
  • 1 ImportJavax.swing.JFrame;2 3  Public classTest {4      Public Static voidMain (string[] args) {5         //create an object, call a constructor method6Teacher tea =NewTeacher ("John Doe");7         8         9Teacher tea1 =NewTeacher ();TenTea.name = "Zhang San"; OneTea1.name = "John Doe"; ATea =tea1; -Tea1.name = "Harry"; -          the System.out.println (tea.name); - System.out.println (tea1.name); -          -     } +}

  • Although there is a "public class name () {}" in the form of a constructor, unlike creating an object with a return value type (such as "Void"), it returns a memory address.
  • In memory, the stored data is divided into stacks and heaps.
  • The stack stores the variables, and the heap stores the objects, all of which have corresponding memory addresses. When the constructor is executed, the initialization property of the data is equivalent to storing the object's data (all) in the memory unit, variables (such as "tea", "Zhang San") to the Stack, and objects (such as "name", "Teach") stored in the heap.
  • The result of "Test.java" above is output: Tea deposit, name "Zhang San" into the heap, all have their own memory address.
    • "Harry
    • Harry
  • When "tea = tea1" is executed, the equivalent of copying TEA1 's memory address to tea,tea points to everything that tea points to, so the result is two "Harry".
  • In "Test.java", the constructor is called as the main function "Tecaher" (File: Teacher.java), and is called once when the "new Teacher ();" Operation is performed.
  • 1 New Teacher ("John Doe");

  • The above can be rewritten as:

    1 Teacher tea; 2 New Teacher ("John Doe");

    Here the constructor "Teacher" is called only once, and "constructor method of the teacher class" is printed only once.

  • file name: Student.java
    1  Public classStudent {2      PublicString name;3      Public intAge ;4     5      Public voidStudy () {6SYSTEM.OUT.PRINTLN ("Student Study");7     }8     9}

  • File name: Unstudent.java
    1  Package com.huaxin.lesson0304; 2 3  Public class extends student{4     5 }

  • As above, it is called inheritance of the class.
  • Definition: Subclasses inherit all non-private properties and methods of the parent class
  • Format: public class subclass name extends parent class name {}
  • "Unstudent.java" students inherit the non-private property of "Student.java" students.
  • That is, all student students all public, college students have.

"Code Note" Java Basics: Inheritance of classes (constructors)

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.