The abstract keyword and realization of learning

1, abstract explanation

Modified abstract class abstract class title

Modified title abstract method abstract methods

A class contains an abstract method, then this class must abstract class

An abstract class can not create object

A class inherits an abstract class, so this time he must abstract methods abstract class, if you do not implement the abstract methods in this class, this class must be abstract class

There may be an abstract class constructor, but the methods are not configured to instantiate an object with

Abstract class constructor is supplied to the sub-class calls, not to initialize

The method can be modified final abstract class

Can have final modifications at the abstract, the modified final method can be called by subclasses, but can not be overridden by subclasses

There may be a member of an abstract class method

Static method can abstract class

abstract class may be modified, the method may be modified, but not modified property; Final class may be modified, the method may be modified, the properties can be modified

Abstract methods: class contains abstract methods must be abstract class, if there is an abstract method in the parent class, then subclass this abstract methods must be implemented, if a subclass does not implement the abstract method, the class must be abstract class

Before jdk1.8, abstract method is not write method body.

2, the parent class

package com.wyq.study;

public abstract class Employer {
	private String name;
	int age;
	public static final int workNo = 10;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
//	public void setAge(int age){
//		this.age = age;
//	}
//	public int getAge(){
//		return age;
//	}
	public Employer(){
		super();
		System.out.println("无参构造"+this.name+"\t"+this.age+"\t"+workNo);
	}
	public Employer(String name,int age){
		super();
		this.name = name;
		this.age = age;
		System.out.println("employer的带参构造"+this.name+"\t"+age+"\t"+workNo);
	}
	public void work(String work){
		System.out.println("在父类中测试员工的工作"+work);
	}
	public  abstract void waitt(String work);
	public final void final1(String sfsd){
		System.out.println("这里是测试final"+sfsd);
	}
}

3, sub-categories

package com.wyq.study;

public class Employee extends Employer{
	
	private String department;
	public void setDepartment(String department){
		this.department = department;
	}
	public String getDepartment(){
		return department;
	}
	public Employee(){
		super();
		System.out.println("这里是子类的无参构造");
	}
	public Employee(String name,int age,String department){
		super(name,age);
		this.department = department;
		System.out.println("这里是无参构造"+super.workNo+"\t"+super.getName()+"\t"+super.getClass()+"\t"+this.age+"\t"+super.workNo);
	}
	public void show(){
		System.out.println(super.getName()+"\t"+super.age+super.workNo);
	}	
	@Override
	public void work(String work) {		
		super.work(work);
		System.out.println("在子类中测试员工的工作是:"+work);
	}
	@Override
	public void waitt(String work) {
		System.out.println("这里是实现的关系"+work);
		
	}
}

4, the test class

package com.wyq.study;

public class TestEmp {
	public static void main(String[] args) throws InterruptedException {
//		Employee e = new Employee("李四",12,"财务部");
//		e.show();
//		System.out.println(e+"\t"+e.age+"\t"+e.getName()+"\t"+e.getDepartment()+"\t"+e.workNo);
		Employee e1 = new Employee("李四",12,"财务部");
//		e.waitt("jsldfj");
		e1.waitt("阿里巴巴集团");
		e1.work("程序员");
		e1.final1("这里是测试finall");
	}
}

5, the output

这里是无参构造10	李四	class com.wyq.study.Employee	12	10
这里是实现的关系阿里巴巴集团
在父类中测试员工的工作程序员
在子类中测试员工的工作是:程序员
这里是测试final这里是测试finall

 

Guess you like

Origin blog.csdn.net/wyqwilliam/article/details/91800748