Java Object-oriented one (encapsulating inherited polymorphic class object methods)

Source: Internet
Author: User
Tags getcolor

1. Encapsulation: The main implementation of the hidden details, to provide access to the user interface, no need to care about the specific implementation of the method.

As below, the human class encapsulates the attributes and behavior of many people;

2. Inheritance: A good implementation of the code reuse, improve the efficiency of programming.

 man { 
Age (attribute one)
Height (property two)
Gender (attribute three)

work (one of the behaviors)
Walk (Act II)
Talk (Act III)
}

 teacher { 
Age (attribute one)
height (attribute two)
Gender (attribute three)

work (one of the behaviors)
Walk (Act II)
Speech (Act III)

Teaching (Act IV)
}


The teacher inherits the person's attribute and the behavior, the extra is teaches the behavior. So it can     be written as a  teacher of  This class of property behavior teaching behavior. 

Teacher extends Person {
Teaching (Act III)
}

3. Polymorphism: Program extensibility and maintainability enhancement.

Polymorphism means that when a method of a parent class is overridden by its subclasses, it can each produce its own functional behavior. In simple terms, polymorphism is characterized by the ability to express many forms.

When a class has many subclasses, and these subclasses override a method in the parent class. So when we put a reference to the object created by the class into the object of a parent class, we get a top-transforming object of that object, then the object that goes up can have many forms when it calls this method.

Objects & Classes

1. An object is an instantiation of a class that uses "Properties" and "methods" to correspond to the "static properties" and "Dynamic properties" that describe things.

2. A class is an abstract concept used to describe an object of the same class, which defines the "static properties" and "Dynamic Properties" that this class of objects should have

3. The appropriate method appears in the appropriate class

I understand the classes, objects, methods:

1. Class (a collection of objects with the same properties and methods): First, it is abstract, not specific, but refers to a certain class.

For example: People, cars, animals, plants.

No specific person (whether adult, child, male or female, as long as the person, it belongs to the category of people)

It does not mean which car (no matter what brand of car, no matter how many wheels, as long as it is a car, it belongs to the class of cars).

  

 Public class car{}

2. Objects (with properties and methods): Instantiation of a class-----------everything is an object--------------

For example: a specific person (me), a specific car (my car), a specific animal (this cat).

  

3. Method (function, member function): a block of code that completes a specific function

For example: People eat, brakes, etc. are methods.

  3.1 Common methods

Format:

modifier Returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...)  {method body statement;    return value;    void type do  not write return}

  

 3.2 Construction Methods

Features:

1. Method name and class name are the same
2. Do not define return value types
3. Can not write return statement

function:

Class to object.

details of the construction method:

When a constructor is not defined in a class, a parameterless construction method is added by default.

When you customize a construction method in a class, the default parameterless construction method disappears (if you need to manually add the method without a parameter).

 Publiccar{PrivateString color; Permission Modifier Private Public voidsetcolor (String color) { This. color =color; }  //This is a set method      PublicString GetColor () {...} //This is a Get method      }     //This is a class, with a common attribute, method; Public Static voidMain (string[] args) {Car a=NewCar ();//instantiate the car class (the legendary new one) A is the class after instantiation, that is, the objectA.setcolor= "Red";
String s = a.getcolor;
System.out.println (s); The color of the output car
}

Java Object-oriented one (encapsulating inherited polymorphic class object methods)

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.