Explain the final keyword

1, the parent class

package com.wyq.study;

public 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  final void waitt(String work){
		System.out.println("我的工作单位是"+work);
	}
}

2, subclass

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);
	}	
}

3, 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);
		Employer e1 = new Employee("李四",12,"财务部");
		e.waitt("jsldfj");
		e1.waitt("阿里巴巴集团");
	}
}

4, the output

employer的带参构造李四	12	10
这里是无参构造10	李四	class com.wyq.study.Employee	12	10
李四	1210
com.wyq.study.Employee@30de3c87	12	李四	财务部	10
employer的带参构造李四	12	10
这里是无参构造10	李四	class com.wyq.study.Employee	12	10
我的工作单位是jsldfj
我的工作单位是阿里巴巴集团

5, summary

. 1) may be modified final properties, the modified final attribute is generally constant and its value can not be changed

2) final method can be modified, the modified final method can not be overridden by subclasses, but subclasses can call

. 3) may also be modified final class, modified final class can not have subclasses, such as math class

4) final and abstract are mutually exclusive relationship

Guess you like

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