Java Classes and Objects (III)

1. Definition and use inheritance

  • Inherited role: carry out the extension of the existing basis, increase the reusability of code
  • Keyword: extends
  • Subclass also become a derived class, parent class is also known as super class (Super Class)

1.1 implementation inheritance

Inheritance Syntax: class 子类 extends 父类
Example: a simple inheritance achieve

class Person{	
	private String name;
	private int age;

	public String getName(){
		return this.name;
	}
	public void setName(String name){
		this.name = name;
	}
	public int getAge(){
		return this.age;
	}
	public void setAge(int age){
		this.age = age;
	}
}
class Student extends Person{
}
public class Test{
	public static void main(String[] args){
		Student stu = new Student();
		stu.setName("张三");
		stu.setAge(20);
		System.out.println("姓名:"+stu.getName()+",年龄:"+stu.getAge());
	}
}
//姓名:张三,年龄:20

In the event of a class hierarchy, the subclasses inherit directly manipulate the parent class may be achieved code reuse, the lowest possible to maintain the parent class the same functionality as
subclasses can also be expanded functions (attributes and methods)
Example: subclasses extensions of

class Student extends Person{
	private String school;

	public String getSchool(){
		return this.school;
	}
	public void setSchool(){
		this.school = school;
	}
}
public class Test{
	public static void main(String[] args){
		Student stu = new Student();
		stu.setName("张三");
		stu.setAge(20);
		stu.setSchool("西安工业大学");
		System.out.println("姓名:"+stu.getName()+",年龄:"+stu.getAge()+",学校:"+stu.getSchool());
	}
}
//姓名:张三,年龄:20,学校:西安工业大学

1.2 inheritance restrictions

  • Subclass before instantiating the object will be first instantiated parent class object . Call the constructor of a subclass after default constructor calls the parent class, sub-class object initialization
    , for example: a subclass object is created
class Person{
	public Person(){
		System.out.println("这是父类的构造方法");
	}
}
class Student extends Person{
	public Student(){
		super();       //可以不写,默认调用无参构造
		System.out.println("这是子类的构造方法");
	}
}
public class Test{
	public static void main(String[] args){
		Student stu = new Student();
	}
}
/*
这是父类的构造方法
这是子类的构造方法
*/

If the parent does not provide the constructor with no arguments, this time using the super () clearly indicate the parent class constructor you want to call

1.3 inherited characteristics

  • Java allows only single inheritance, do not allow multiple inheritance, a subclass can only inherit from a parent class
    Example: Wrong inheritance
	class A{}
	class B{}
	class c extends A,B{}

Example: in order to achieve an operation C of A and B, may be employed multilayer inheritance

	class A{}
	class B extends A{}
	class C extends B{}

** summarize: ** Java does not allow multiple inheritance, but it allows multiple layers inheritance

When performing the inheritance, the subclass inherits all the parent class structures (including private attributes, constructors, the conventional method), but belongs to all non-proprietary operating Inherited display (can be called directly), all belonging to the private attributes implicit actions (by the way setter and getter calls)

Example: Show Inherited implicit inheritance

class Person{
	private String name;

	public String getName(){
		return this.name;
	}
	public void setName(String name){
		this.name = name;
	}
}
class Student extends Person{
	public void fun(){
		System.out.println(this.getName());
	}
}
public class Test{
	Student stu = new Student();
	stu.setName("张三");
	stu.fun();
}
//张三

At this time, the properties of the parent class inherited by subclasses, but subclasses can be used are all non-private operated and all the private operation can not be used directly, so called implicit inheritance

2. override (override)

  • Definition: subclass definition with the same parent class attributes and methods

2.1 Method of override

  • Subclass defines the parent class and method name, type and number of parameters exactly the same way
  • Subclasses override the method can not have more stringent than the parent access
  • public> protected> default (default package)> private
    example: Simple overwrite
class Person{
	public void print(){
		System.out.println("这是父类的方法");
	}
}

class Student extends Person{
	public void print(){
		System.out.println("这是子类的方法");
	}
}

public class Test{
	public static void main(String[] args){
		new Student().print();
	}
}
//这是子类的方法

When a method is invoked, if the method has been overwritten by a subclass, the call must be written coating method
Examples: private use defined parent class, subclass using public override

class Person{
	public void fun(){
		this.print();
	}
	private void print(){
		System.out.println("这是父类的方法");
	}
}

class Student extends Person{
	public void print(){
		System.out.println("这是子类的方法");
	}
}
public class Test{
	public static void main(String[] args){
		new Student().fun();
	}
}
//这是父类的方法

When the object is new a subclass to call fun () method, the subclass is not the way to parent looking for, although the parent class print () method is private, but the fun parent class () method and the parent class print () method in a class can be called
for example: the method of the parent class subclasses override

class Person{
	public void fun(){
		this.print();
	}
	public void print(){
		System.out.println("这是父类的方法");
	}
}
class Student extends Person{
	public void print(){
		System.out.println("这是子类的方法");
	}
}
public class Test{
	public static void main(String[] args){
		new Student().fun();
	}
}
//这是子类的方法

If the quilt class override the parent class method, then the call is a subclass method

2.2 difference overloaded (overload) and the override (override) a

the difference Overload Overwrite
concept The same method name, and the number of parameters of different types of The method name, return type and number of identical
range A class Inheritance
limit No permission required Subclasses override the method can not have more restrictive access than the parent class

2.3 super keyword

  • Overwriting operation is performed, the sub-class may be used super.方法/super.属性a method call the parent class or attribute
    of example: method of using the same name as the parent class calls super
class Person{
	public void print(){
		System.out.println("这是父类的方法");
	}
}

class Student extends Person{
	public void print(){
		super.print();
		System.out.println("这是子类的方法");
	}
}
public class Test{
	public static void main(String[] args){
		new Studnet().print();
	}
}
/*
这是父类的方法
这是子类的方法
*/

And this is the difference between 2.4 super

the difference this super
concept Accessing properties and methods of the class Properties and methods of the parent class subclasses access
Look First find of this class, this class does not have to look for the parent class This does not look like, but calling the immediate ancestor
special It represents the current object no

3. final keyword

1. The terminator is known as final in Java

  • Use final modification classes, methods, properties
  • final member variables must be initialized or initialized in the constructor when it was declared, or the compiler will complain
  • With the final defined classes can not have subclasses
  • Once a modified final class, plus all of the methods will be final (not including the member variables)
	final class A{}   //A类不能有子类
  • Final use methods defined subclasses can override
class A{
	public final void fun(){}
}
  • Variables final definition has become a constant, constant must be assigned at the time of declaration , and can not be modified
	public final int a = 100;
  • Defines constants (public static final), the constant all capital letters, separated by a plurality of inter-word _
	public static final MAX_AGE = 120;

2. Data type conversion

  • Two operands, is a double , the other is automatically promoted to double , the result is double
  • Two operands, as a float , the other is automatically elevated to float , the result is float
  • Two operands, is a long , the other is automatically elevated to the long , the result is long
  • Two operands are byte, short, int, char, both operands are converted to type int, int, and the result is
  • The final type of modification does not change
	byte b1 = 1, b2 = 2, b3,b6,b8;
	final byte b4= 4,b5 = 6, b7 = 9;
	public void test(){
		b3 = (b1 + b2);  // byte = int  精度损失
		b6= (b4+ b5);    //byte = byte
		b8= (b1 + b4);    //byte = int  精度损失
		b7= (b5+ b2);     ////byte = int  精度损失
	}

4. Polymorphic

More than 4.1-state performance

In Java, polymorphism core performance of two things:

  • Polymorphism method:
    • Overload 1. Method: the same method name can be removed in different ways according to the list of different parameters thereof
    • 2. The method of overwriting: a parent class with methods, may be implemented differently according to the different instances of subclasses
  • Object polymorphism:
    • 1. Transition object up (automatic):父类 父类对象 = 子类实例
    • 2. downcasting objects (mandatory): 子类 子类对象 = (父类)父类实例
      Example: upcast
class Person{
	public void print(){
		System.out.println("这是父类的方法");
	}
	public void fun(){
		System.out.println("只有父类有");
	}
}
class Student extends Person{
	public void print(){
		System.out.println("这是子类的方法");
	}
}
public class Test{
	public static void main(String[] args){
		Person per = new Person();     //new的是Person类的对象
		per.print();                   //调的是Person的方法

		Person per1 = new Student();   //new的是Student对象,在这之前已经实例化Person类
		per1.print();                   //调用的是被父类覆写的方法

		Person per2 = new Student();    
		per2.fun();					//子类没有,调用的是父类的方法
	}
}
/*
这是父类的方法
这是子类的方法
只有父类有
*/

Regardless of whether there has been restructuring, the core is: you are the new class of
examples: downcast

class Person{
	public void print(){
		System.out.println("这是父类的方法")
	}
}
class Student extends Person{
	public void print(){
		System.out.println("这是子类的方法");
	}
	public void fun(){
		System.out.println("只有子类有");
	}
}
public class Test{
	public static void main(String[] args){
		Person per = new Student();
		per.print();

		Student stu = (Student)per;
		stu.fun();
	}
}
/*
这是子类的方法
只有子类有
*/

Summary: upcast object has a core purpose: a unified operating parameters
polymorphism Summary:

  • The core target is overwritten polymorphism method
  • Through a unified upcast reception parameters, downcast implementation calls to expand the subclass method
  • Two not related to the class object can not make the transformation, will produce ClassCastException

Guess you like

Origin blog.csdn.net/mi_zhi_lu/article/details/88344070