Java learning notes 9 (Object-Oriented 2), java learning notes

Source: Internet
Author: User

Java learning notes 9 (Object-Oriented 2), java learning notes

Proximity principle:

When the names of the variables in the method and member variables in the class are the same, the variables in the method take effect when the class method is called. If no variables are defined in the method, the variables in the member variables will be searched.

Therefore, this keyword is proposed to differentiate duplicate names.

Public class Person {// age of the name of a Person, member variable private String name; private int age; public void setAge (int a) {if (a <0 | a> 100) {age = 20 ;}else {age = a ;}} public void setName (String n) {name = n ;}// define a method to obtain age public int getAge () {return age;} public String getName () {return name;} // public void speak () {String name = ""; int age = 16; System. out. println (this. age + "years old" + this. name + "talking ");}}

 

Public class PersonTest {public static void main (String [] args) {Person p = new Person (); p. setName ("Zhang San"); p. setAge (80); System. out. println (p. getAge (); System. out. println (p. getName (); p. speak ();}}

 

If this is not set here, the output is Li Si. If this is set, the output is Zhang San.

Note that this isCallerObject

 

This memory analysis:

1. Run the main program on the stack, store the space in the heap to the Person object, and assign the default value to the member variables.

2. when p calls the speak method, the speak is executed in the stack. The caller is the p object, and this is the p object itself. Therefore, p transmits the object address to this, so this also points to this memory space.

 

So we found that the Person class just now will be more standardized:

Public class Person {private String name; private int age; public void setAge (int a) {if (a <0 | a> 100) {this. age = 20;} else {this. age = a ;}} public void setName (String n) {this. name = n;} public int getAge () {return age;} public String getName () {return name;} public void speak () {String name = ""; int age = 16; System. out. println (this. age + "years old" + this. name + "talking ");}}

 

Inheritance:

Class Inheritance refers to building a new class based on an existing class. The new class constructed is called a subclass, and the existing class is called a parent class.

The subclass automatically owns all the attributes and methods that can be inherited by the parent class.

Example:

Public class Employee {String name; public void work () {System. out. println ("Employee" + name + "working ");}}
public class Develop extends Employee{    public void print(){        System.out.println(name);    }}
Public class Test {public static void main (String [] args) {Develop d = new Develop (); d. name = "James"; d. print (); d. work ();}}

The inherited features and advantages are shown in this example.

1. Improve code reusability and development efficiency

2. The emergence of inheritance creates a relationship between classes and provides the premise of polymorphism.

 

Note:

Java only supports single inheritance, but does not support multiple inheritance because of security risks. For example, when two parent classes have duplicate names

You can use continuous inheritance (multiple inheritance) to implement multiple inheritance. This method is valid.

 

Member variable changes:

If the variable defined by the subclass has the same name as the parent class, the variable of the subclass prevails when the subclass is called. If the subclass does not have this variable, search for it in the parent class, however, there is a super keyword that can be specified to be searched in the parent class.

public class Fu {    int i = 1;}
public class Zi extends Fu {    int i = 2;    public void show(){        System.out.println(super.i);    }}
public class Test {    public static void main(String[] args) {        Zi z = new Zi();        z.show();    }}

At this time, the output result is 1. The super knowledge will be detailed later.

 

Subclass override parent class method:

When the same method as the parent class appears in the subclass, it is called the subclass to override the parent class method.

Use super. To call the function of the parent class, and then write new functions.

Note: to override the parent class method, make sure that the permission is greater than or equal to the parent class permission (public> protected> default> private)

 

Abstract class:

Only declaration, not Definition

Abstract METHODS must exist in abstract classes.

Abstract class. The object cannot be instantiated (new cannot be used) because the abstract class does not have a subject and cannot be run.

Use of abstract classes: You must define a class to inherit from it, override the abstract method, create its subclass object, and force the subclass override method

public abstract class Person {    public abstract void work();}
Public class Teacher extends Person {public void work () {System. out. println (" ");}}
Public class Engineer extends Person {public void work () {System. out. println ("Engineer at work ");}}
public class Test {    public static void main(String[] args) {        Teacher t = new Teacher();        Engineer e = new Engineer();        t.work();        e.work();    }}

 

Output:

The teacher is at work.
Engineer at work

 

Abstract classes are mainly used to learn their design ideas:

The role of an abstract class: an inherited System

Abstract classes force subclass override abstract methods to ensure the integrity of system functions

 

Notes for abstract classes:

1. abstract classes must have abstract methods. abstract classes do not have abstract methods.

2. abstract keywords cannot coexist: private, final, static

3. the abstract class must be a parent class.

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.