Chapter V java Classes and Objects

1. Object Oriented

java is object-oriented language. Object-Oriented (OOP ie Object Oriented Programming) is an idea, that everything can be seen as an object. We look at an analogy analogy to animals as an example, the animal can be seen as a general term for a class of things, to see as a class. Which may be a tiger, a lion, or people ... This is a specific implementation of this class of animals that object. Animals have to eat, running behavior, we can see the method as an example, the eyes and nose and ears, these are the attributes instances. All in all, object-oriented thinking is a typical look simple, but in-depth understanding of the type of hard, take a long time of exploration and a significant number of programming experience in order to fully grasp, to understand the extent of OOP programmers with the code level design and architecture They are closely related.

2. The object-oriented features

2.1 Inheritance

Inheritance is one of the basic features of object-oriented inheritance mechanism allows the creation of hierarchical level classes. Inheritance is the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) instance fields and methods having a parent class or subclass inherits methods from parent classes, subclasses that have the same parent class behavior. FIG Similar following:
 Here Insert Picture Description

2.2 package

Package is to hide the object properties and implementation details, only open to the public interface to control read and modify the program attribute access level, abstract data obtained and acts (or function) combine to form an organic whole, i.e. the source code data and operation data of organically combined to form a "class", the data and functions which are members of the class.
The purpose of the package is to enhance security and simplify programming, the user need not know the specific implementation details, but only through external interfaces to specific access rights to the use of
members of the class.

Over 2.3-state

Capacity having a plurality of different polymorphic forms or forms of the same behavior. Refers to a method of the same class instances (objects) have different forms in different situations. Of polymorphism so that objects having different internal structures can share the same external interface. This means that, although different specific actions for different objects, but through a common class, they (those operations) can be invoked in the same way.

2.3.1 polymorphic advantages:

  1. Eliminate the coupling relationship between type
  2. Substitutability
  3. Scalability
  4. Interface sex
  5. flexibility
  6. Simplicity

Three necessary conditions for the existence of polymorphism: Inheritance override (redefine subclasses of the parent class method inherited from the parent class after) references to the parent class subclass object

Note: The object-oriented concepts and characteristics of the three, although the primary concept, but to really understand it is to be a long period of accumulation + sentiment regarding this concept to explain in detail to re-design patterns and design principles behind a will.

This paragraph is taken from this blog

3. similarities and differences between the abstract class and interfaces

3.2 in common

  1. Not be instantiated.
  2. Implementation class and subclass of abstract class interface implements all methods only interface or abstract class can be instantiated after

3.2 difference

  1. Abstract methods defined in an interface can not be implemented method either abstract class defines the abstract methods, a method may also be implemented.
  2. Single inheritance, to achieve. Can implement multiple interfaces, you can only inherit an abstract class.
  3. Interface emphasized that the function, emphasized that the abstract class affiliations.
  4. All member variables interface is public static finalstatic can not be modified, of course, must be initialized. All interface methods are public abstractpublic abstract. And can not have constructor. An abstract class is more freedom, and almost ordinary class can have abstract methods can not, you can have the normal way, you do not.

3.3 abstract classes and interfaces focus

Abstract class defines 3.3.1

An abstract class is often used to characterize the analysis of problem areas, design drawn abstraction is a series of looks different, but essentially the same abstract concepts concrete.

3.3.2 Role abstract class

1, of the type used to hide the hide, we can construct an abstract description of the behavior of a set of fixed, a possible behavior can have any specific implementation. This abstract description is an abstract class. (Reference polymorphism)
2, object behavioral function for expanding
the group of any possible performance embodied all possible subclasses, modules may operate an abstract class, since the module relies on a fixed abstract class, he can not be modified. Simultaneously derived by this abstract class, expanding the functional behavior of this module. (Refer to the principle of open closed)

3.3.3 Interface definitions

Refers to entities provide their own interfaces to the outside of one kind of abstraction thereof (may be another entity), methods for separating the internal and external communication operations, that it can be modified without affecting the way inside the external interact with other entities.

3.3.4 interface role

The reason Java single inheritance, so the need Quxianjiuguo inheritance as a supplement.
The program module cured contract, reducing the coupling. To split out a number of functions, realized according to the contract and dependence. (Dependency Inversion Principle)
defines the interface help standardize code. (Interface Separation Principle)

Example 4

More obscure concept of comparative examples we give an animal to look deeper impression

4.1 Interface

Let's create a life-cycle interface LifeCycle, all animals have a life cycle, so we abstract such an interface

package com.hqa;

public interface LifeCycle {
	/**
	 * 生
	 */
	void born();
	
	/**
	 * 老
	 */
	void old();
	
	/**
	 * 病
	 */
	void ill();
	
	/**
	 * 死
	 */
	void die();
}

4.2 abstract class

Create an abstract class animal

package com.hqa;

public abstract class Animal implements LifeCycle{
	
	@Override
	public void born() {
		System.out.println("出生了...");
	}
	
	@Override
	public void old() {
		System.out.println("老了...");
	}

	@Override
	public void ill() {
		System.out.println("生病了...");
	}

	@Override
	public void die() {
		System.out.println("死了...");
		
	}
}

4.3 felines class

package com.hqa;

public class CatKindAnimal extends Animal{

   /**
    * 构造函数
    */
   public CatKindAnimal(String name){
     this.name = name;
   }

   /**
	 * 姓名
	 */
	public String name; //共有属性
	
	/**
	 * 花色
	 */
	private String color; //私有属性

	/**
	 * 拥有技能 爬树
	 */
	public void tree(){
		//TODO ..
	}


}

4.4 Fish

package com.hqa;

public class FishKindAnimal extends Animal{
	
	/**
	 * 拥有技能游泳
	 */
	public void swim(){
		//TODO ..
	}

}

4.5 immortal class

package com.hqa;

public class SuperAnimal extends Animal{
	
	/**
	 * 神仙是长生不死的,可以通过重写die方法来实现
	 * 但是这样做并不是一个好的设计,后面我们会讲到接口隔离原则
	 */
	@Override
	public void die() {
		System.out.println("神仙不会死...");
	}

}

A class may include the following types of variables:

Local variables: variables defined in the method, the constructor is called or statement block local variables. Variable declaration and initialization are in the process, after the end of the method, the variable will be automatically destroyed.
Member variables: class member variables are defined, the variables other than the method body. This variable is created when an object is instantiated. Member variable can be accessed statement block class methods, class-specific methods and configurations.
Class variables: Class Variable also in the class declaration, the method outside the body, but must be declared as static type.

4.6 Examples

The object is a class created. In Java, use the keyword new to create a new object. Create an object requires the following three steps:
declare: declare an object, including the object name and object type.
Examples: Use the new keyword to create an object.
Initialization: Use to create a new object, it will call the constructor to initialize the object.

Let's look at an example of:

package com.hqa;

public class Test {
	
	public static void main(String[] args)  {
		//实例化了一个名叫tom的猫科动物
		CatKindAnimal cat = new CatKindAnimal("TOM");
		cat.born();
		cat.die();
		
		//实例化了一个神仙
		SuperAnimal sup = new SuperAnimal();
		sup.die();
	}

}

Run can see the following results:

出生了...
死了...
神仙不会死...
Published 10 original articles · won praise 6 · views 3561

Guess you like

Origin blog.csdn.net/qq_29145405/article/details/103979503