Interface test [encapsulation, inheritance, polymorphism]

1, the parent class

package com.wyq.study;

public class Pers {
	//书写类
	//书写属性
	private String name;
	private int age;
	//提供共有的取值赋值方法
	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 Pers(){
		super();
		System.out.println("这里是父类的无参构造");
	}
	//带参构造
	public Pers(String name,int age){
		super();
		this.name = name;
		this.age = age;
		System.out.println("这里是父类带参的构造方法"+this.name+"\t"+this.age);
	}
	//父类的普通方法 
	public void speak(String language){
		System.out.println("这里是父类的测试:\n"+name+"今年"+age+"岁了,正在说"+language);
	}
}

2, Interface 1

package com.wyq.study;

public interface Dance {
	//接口中的属性
	public static final int DanceAge = 10;
	//接口中的抽象方法
	public abstract void dan();
}

2, Interface 2

package com.wyq.study;

//抽象类
public interface Sing {
	//抽象类中的属性
	public static final String danceName = "草原";
	//抽象类中的普通方法
	public abstract void sin(String sinName);
}

3, the subclass inherits [parent, the parent class rewriting, implementation subclasses, subclasses implemented method]

package com.wyq.study;

public class LaoWang extends Pers implements Dance,Sing{
	private String job;
	public void setJob(String job){
		this.job = job;
	}
	public String getJob(){
		return job;
	}
	public LaoWang(){
		super();
		System.out.println("这里是子类的无参构造");
	}
	public LaoWang(String name,int age,String job){
		super(name,age);
		this.job = job;
		System.out.println("这里是子类的带参构造方法,其参数为:"+super.getName()+"\t"+super.getAge()+"\t"+job);
	}
	
	@Override
	public void speak(String language) {
		super.speak(language);
		System.out.println("这里是重写父类的方法,老王会说"+language+"\t"+super.getName()+"\t"+super.getAge()+"\t"+job);		
	}
	@Override
	public void dan() {
		System.out.println("测试子类中的跳舞方法"+job+"\t"+super.getName()+"\t"+getAge()+this.DanceAge);		
	}
	@Override
	public void sin(String sinName) {
		System.out.println("这里是测试唱歌的方法"+danceName+"\t"+this.DanceAge);
		
	}
	
}

4, the test class

package com.wyq.study;

public class TestLaoWang {
	public static void main(String[] args) {
		/**
		 * 使用多肽,父类new子类对象
		 * 这种情况只能调用父类中的方法
		 */
		Pers p = new LaoWang("张三",18,"程序员");
		p.speak("昌平话");
		System.out.println("这里是测试多肽"+p.getName()+"\t"+p.getAge()+"\t");
		System.out.println("____________________________");
		/**
		 * 使用接口进行new
		 * 这种情况只能定义接口中的方法
		 */
		Dance d =  new LaoWang("李四",20,"公务员");
		d.dan();
		System.out.println("这里是测试接口类的方法"+d.DanceAge);
		System.out.println("***************************");
		Sing s = new LaoWang("王五",22,"老师");
		s.sin("旭日阳刚");
		System.out.println("这里是测试唱歌的方法"+s.danceName);
		/**
		 * 自己创建自己的对象
		 * 这种情况下,可以调用重写/实现父类中的方法,还可以调用实现接口的方法
		 */
		LaoWang lw = new LaoWang("马六",24,"交警");
		lw.dan();
		lw.sin("姑娘我爱你");
		lw.speak("英国话");
		System.out.println("这里是测试子类自己"+lw.getName()+"\t"+lw.getAge()+"\t"+lw.getJob()+lw.danceName+"\t"+lw.DanceAge);
		
	}

}

5, the result output

这里是父类带参的构造方法张三	18
这里是子类的带参构造方法,其参数为:张三	18	程序员
这里是父类的测试:
张三今年18岁了,正在说昌平话
这里是重写父类的方法,老王会说昌平话	张三	18	程序员
这里是测试多肽张三	18	
____________________________
这里是父类带参的构造方法李四	20
这里是子类的带参构造方法,其参数为:李四	20	公务员
测试子类中的跳舞方法公务员	李四	2010
这里是测试接口类的方法10
***************************
这里是父类带参的构造方法王五	22
这里是子类的带参构造方法,其参数为:王五	22	老师
这里是测试唱歌的方法草原	10
这里是测试唱歌的方法草原
这里是父类带参的构造方法马六	24
这里是子类的带参构造方法,其参数为:马六	24	交警
测试子类中的跳舞方法交警	马六	2410
这里是测试唱歌的方法草原	10
这里是父类的测试:
马六今年24岁了,正在说英国话
这里是重写父类的方法,老王会说英国话	马六	24	交警
这里是测试子类自己马六	24	交警草原	10

6, summed up

1) a subclass can inherit a parent class, but can implement multiple interfaces N

2) constructor not permitted in interfaces, because the interface is not a class

3) interface should be variable public static final type, often public static final omitted interface final variable is modified, it must be initialized

4) Before jdk1.8, abstract methods abstract class interface, or it is not allowed the method body

5) After jdk1.8, the method can be defined in the interface, and the method comprising, single default keyword is required to be written

6) the relationship between classes and interfaces that class must implement the interface, method interface implemented

7) If there is both a class inheritance, and there realize the relationship, then go to the front extends inheritance, implements realized after

8) abstract class abstract method may be modified by abstract, abstract can be omitted

9) If all the methods in a class are abstract methods, then this class is the interface

10) After the defined interfaces, public abstract can be omitted

11) interface are public by default abstract methods

Guess you like

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