Java Basics-Object oriented

Source: Internet
Author: User

Object


Object:

is an instance of a class (the realization that everything that exists in the world can be called an object)

Class:
Class is an abstract description of an object


Steps:

1. Define a class (to describe a person:)
(* Person: having characteristics and behavior)
2. Create objects based on class----new
* Object Type Object name = new object type ();

* Normally only objects can point out properties and methods

Three characteristics of object-oriented: encapsulation inheritance polymorphism

Package : privatize the properties of a class to provide a public get/set access method

Cases
Encapsulation of attributes
private String name;
private int age;

Member Methods
GetXxx ()/setxxx ()
Encapsulation of the object's approach to the outside world
Public String GetName ()
{
return name;
}

public void SetName (String name)
{
THIS.name = name;
}

public int Getage ()
{
return age;
}

public void Setage (int.)
{
This.age = age;
}


Note: Get and set appear in pairs

The role of encapsulation
The data encapsulation feature of ① object completely eliminates the problems caused by the separation of data and operation in the traditional structure method, improves the reusability and maintainability of the program, and reduces the burden of the programmer to keep the data and operation content.

The ② object's data encapsulation feature also separates the private data of the object from the public data, protects the private data, reduces the possible inter-module interference, and achieves the purpose of reducing the complexity of the program and improving controllability.

inheritance : Inheriting existing classes (subclasses, derived classes) with new classes (parent class superclass base class), new classes can directly use the public methods and properties of existing classes

role: Submit code reuse rate Reduce unnecessary code

Keyword:extends

inheritance can only be inherited in a single inheritance (parent class may have many subclasses but subclasses can have only one immediate parent class)

after the subclass overrides the method of the parent class will call the subclass's own method first

Inheritance mainly implements reusing code, saving development time.

1. Inheritance in C # conforms to the following rules:

      1. Inheritance is transitive. If c derives from B and B derives from a, then C inherits not only the members declared in B, but also the members of a. The object class acts as the base class for all classes.
      2. The derived class should be an extension to the base class. A derived class can add new members, but cannot remove the definition of a member that has already been inherited.
      3. Constructors and destructors cannot be inherited. Other members, regardless of the type of access they define, can be inherited. The way members in a base class can access only determines whether derived classes can access them.
      4. A derived class can overwrite an inherited member if it defines a new member with the same name as the inherited member. However, this is not because the derived class has deleted these members, but they are no longer accessible.
      5. Classes can define virtual grammars, virtual attributes, and virtual index indicators, and its derived classes can overload these members, enabling classes to demonstrate polymorphism.

2. New keyword

If a protected or public method is declared in the parent class that does not have a friend adornment, a method with the same name is also declared in the subclass. You can hide methods in the parent class with new. (Not recommended)

3. Base keyword

The base keyword is used to access the members of the base class from a derived class:

      1. Calls a method on the base class that has been overridden by another method.
      2. Specifies the base class constructor that should be called when creating an instance of a derived class.

Cases

Class Person {public ' () {}}class man extends ()}
The class man inherits from the person class, so that the person class is called the parent class (the base class), and the man class is called a subclass
    1. inheritance can only be inherited in a single inheritance (parent class may have many subclasses but subclasses can have only one immediate parent class)
    2. after the subclass overrides the method of the parent class will call the subclass's own method first
    3. If an inheritance relationship exists for two classes, the subclass automatically inherits the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass

Report:

{}: code block

{} defined in method: normal code block

{} defined in class: building block

{} defined in class and modified by static : Static code block

access Modifiers:4

Public: The entire project can be accessed

producted: Sub-class under the same package or under a different package

Default (Write Nothing): Under the same package

Private: Current class

Polymorphic: Multiple forms of the same instance

Conditions in which polymorphism occurs: in an inheritance relationship

    1. Overrides of methods in an inheritance relationship, overloading of methods under the same class
    2. The parent class's reference to the entity of the subclass (an upward transformation in an inheritance relationship, and a downward transition)

Polymorphic: A reference to a parent type can point to an entity (object) of a subtype

//Up transformation, converting subtypes to parent type

Person person=new Student (); When calling a method using polymorphic mode, first check if there is a method in the parent class, and if not, compile the error;

//down transformation, convert parent type to subtype

Student Stu =new (Student)person; Coercion type conversions are required if you want to invoke a method that is in a subclass that is not in the parent class

Java Basics-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.