day 9 abstract class, and interface design patterns template

day 9

Abstract class
abstract class, abstract. He no specific examples, if there is a way to abstract a class, then he must be abstract class

Q1, establishing abstract class Animal, and the establishment of category Dog, based Fish, moves to print out the dog and fish.

package day09;

public abstract class Animal {//只写方法不写方法体
	public abstract void test();
	
	public abstract void move();
		
}




class Dog extends Animal{//继承后会报错,在eclipse里,单击类名Dog,点击Add inimplemented methods就可以自动添加重写方法

	@Override
	public void test() {
		
		
	}

	@Override
	public void move() {//重写move
		System.out.println("狗的移动方式是撒欢");
		
	}
	
}



class Fish extends Animal{

	@Override
	public void test() {
		
		
	}

	@Override
	public void move() {
		System.out.println("鱼的移动方式是蹦跶");
		
	}
	
}

Dog and a new fish and the main method used in the embodiment .move
print out the dog and moves the fish.

	Dog d = new Dog();
	d.move();
	Fish f = new Fish();
	f.move();

. Q2 write an Employee class, declared as an abstract class that contains the following three properties: name, id, salary.
Provide the necessary constructors and abstract methods:. Work () for the Manager class, he not only employees, but also has a bonus (bonus) of the property, please use the inherited ideas,
design CommonEmployee Manager class and class, the class required to provide the necessary property access methods.

package day09;

public abstract class Employee {
	
	String name;
	int id;
	double salary;
	
	public Employee(){
		
	}
	
	public abstract void work();
}


class CommonEmployee extends Employee{

	public void setComminEmployeeInfo(String name,int id,double salary) {
		super.name = name;
		super.id = id;
		super.salary = salary;
	}
	public void getComminEmployeeInfo() {
		System.out.println(super.id);
		System.out.println(super.name);
		System.out.println(super.salary);
	}
	@Override
	public void work() {
		System.out.println("这是一个普通员工");
	}
}
	
	
class Manager extends Employee{
	double bonus;
	
	public void setManagerInfo(String name,int id,double salary,double bonus) {
		super.name = name;
		super.id = id;
		super.salary = salary;
		
		this.bonus = bonus;//注意,这里的bonus不要使用super.
	}
	
	public void getManagerInfo() {
		System.out.println(super.name);
		System.out.println(super.id);
		System.out.println(super.salary);
		System.out.println(this.bonus);
	}
	
	@Override
	public void work() {
		System.out.println("这是一个领导");
		
		
	}
	
}

In the main method call

CommonEmployee ce = new CommonEmployee();
	ce.setComminEmployeeInfo("zhangsan", 113, 6300.15);
	ce.getComminEmployeeInfo();
	ce.work();
	
	Manager m = new Manager();
	m.setManagerInfo("lisi",114, 123.21, 45401.14);
	m.getManagerInfo();
	m.work();

Template model designed
abstract class is reflected in the design of a template pattern, a plurality of abstract class as a subclass of a generic template
when a portion of the internal function is implemented to determine, part of the implementation is uncertain.
In this case it can be uncertain to expose portions let subclass implementation.
write an abstract superclass, parent class provides a common method for multiple subclasses, and the one or more methods to achieve its subclasses, which is a kind of template

package day09;
/**
 * 模板设计模式
 * @author Administrator
 *
 */
public abstract class Template {
	public abstract void code();
	
	public final void getTime() {
		long start = System.currentTimeMillis();
		code();
		long end = System.currentTimeMillis();
		
		System.out.println("code方法执行的时间: " + (end - start));
	}
}



class TestTemp extends Template{

	@Override
	public void code() {
	
		for(int i = 0;i < 500; i++) {
		
		}
		
	}
	
}

In the main method, a new new TestTemp object using .getTime () method of retrieval results

Interface
interface features:
use interface to define
all member variables are the default interface is modified by the public static final
all interface methods are modified from the default public abstract
interface is not a constructor
interface uses a multi-layered inheritance
public interface Runner {
int = ID. 1;
void Start ();
public void RUN ();
void STOP ();
}
corresponds to the
public interface Runner {
public static int ID = Final. 1;
public abstract void Start ();
public abstract void RUN () ;
public abstract void STOP ();
}

Right in the package when the new class interface is not new, but the interface
when the test interface, use the implements keyword after the class name

package day09;
/**
 * 子类可以继承父类,只能继承一个父类
 * 类可以实现多个接口,多个接口用,分隔
 * @author Administrator
 *
 */
public class TestInImpl implements TestIn,TestIn1{

	@Override
	public void test() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void test1() {
		// TODO Auto-generated method stub
		
	}

}

Interface interface can be inherited
if a class that is inherited from the parent class, and implements the interface
then the first inheritance , and then realized

The new company arrived today, tomorrow to continue learning progress slow interfaces to learn

Released eight original articles · won praise 0 · Views 80

Guess you like

Origin blog.csdn.net/Reishinj/article/details/105059826