20 using the abstract class java object-oriented abstract of

/*

  • Abstract classes and abstract methods, abstract Keywords
  • abstract classes and methods may be modified, the modified variable t is not, block, builder,
  • Private methods can not be modified with abstract, static methods, final methods, final classes.
  • 1, with the abstract keyword to modify a class, the class is called abstract class.
  • 1.1 abstract class can not be instantiated
  • 1.2 class constructor must be easy to call when the subclass is instantiated.
  • 1.3 applications will provide a subclass of an abstract class, so subclass object is instantiated.
  • 2, with a modified abstract method called abstract method.
  • 2.1 only abstract method declarations, no method thereof, there is no {},
  • 2.2 contains abstract methods must be abstract class, or the class will be instances, it may be retrieved abstract methods. There is not necessarily an abstract class abstract methods.
  • Subclasses must override 2.3 abstract class is the abstract parent class, and a method thereof.
  • If no override all abstract methods, still abstract class
  • 3, abstract classes when you can use an anonymous call to the class, in the case can be called only once by
  • new abstract class name () {} method name (); Create an anonymous manner abstract class, an abstract method of an internal write {} overridden.
  • 4, Template Method design pattern
  • 4.1 abstract class embodies a kind of template pattern design, abstract classes as subclasses of a generic template
  • Subclass is extended on the basis of the transformation of the abstract class, subclass but will retain overall behavior of an abstract class
  • 4.2 As part of the implementation is to determine the internal function of a part of implementation is uncertain. Then you can go to the uncertainty is partially exposed, so that subclasses to achieve.
  • 4.3 In other words, an algorithm implemented in software development, the entire steps are fixed, general, these steps have been written in the parent class. But some parts of the volatile, volatile portions can be abstracted out for different subclasses to implement. This is a kind of template mode.
  • 4.4 Template Method design pattern is often used to get the programming mode. Each framework class library has his
  • Shadow, such as common are:
    * l access to the database package
    *  Junit unit testing
    *  JavaWeb regarding the Servlet doGet / doPost method call
    *  Hibernate template program
    *  Spring in JDBCTemlate, HibernateTemplate etc.

*/

package object_chapter2;

import java.util.Calendar;
import java.util.Scanner;

public class Object_Abstract {
  public static void main(String[] args) {
	//非匿名类非匿名对象调用方式
	Dog d = new Dog();
	d.shout();
	//非匿名类匿名对象调用方式
	new Dog().eat();  
	//匿名类非匿名对象调用方式
	Animal a = new Animal() {

		@Override
		void eat() {
			// TODO Auto-generated method stub
			System.out.println("bird eat bugs");
		}

		@Override
		void shout() {
			// TODO Auto-generated method stub
			System.out.println("渣渣!!");
		}
		
	};
	a.eat();
	a.shout();
	//匿名类匿名对象的调用
	new Animal() {

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

		@Override
		void shout() {
			// TODO Auto-generated method stub
			System.out.println("嘶嘶嘶!!!");
		}
		
	}.shout();
	  //测试模板设计模式
	 Timer t = new FindPrime();
	  t.spendTime();
	
	  //测试练习
	  //获取当前月份方式1
//	  Scanner scan = new Scanner(System.in);
//	  System.out.println("请输入当前月份");
//	  int month = scan.nextInt();
	//获取当前月份方式2
	  Calendar c = Calendar.getInstance();//Calendar是JDK中 提供的关于日期的抽象类
	  int month = c.get(c.MONTH) + 1; //月份数值从0到11,转换到正常数值需要加1.
	  System.out.println("当前月份是:" + month + "月");
	  Employee[] employeeList = new Employee[4];
	  employeeList[0] = new HourlyEmployee(1001, "join", new BirthDate(2000, 10, 1), 50, 256);
	  employeeList[1] = new SalariedEmployee(1002, "jake", new BirthDate(1995, 1, 11), 14000);
	  employeeList[2] = new HourlyEmployee(1003, "pork", new BirthDate(2002, 2, 1), 55, 244);
	  employeeList[3] = new SalariedEmployee(1004, "tony", new BirthDate(1990, 2, 11), 19000);
	  for (int i = 0; i < employeeList.length; i++) {
		 System.out.println(employeeList[i].toString());
		 if(month == employeeList[i].getBirthday().getMonth()){
			 System.out.println("生日福利加1000元");
		 System.out.println("当月工资是: " + (employeeList[i].earning() + 1000));
		 }else {
	     System.out.println("当月工资是: " + employeeList[i].earning()); 
		 }
	}
}
  
}

abstract class Animal{
	private String name;
	private int age;
	abstract void eat();
	abstract void shout();
}

class Dog extends Animal{

	@Override
	void eat() {
		// TODO Auto-generated method stub
		System.out.println("dog eat bones");
	}

	@Override
	void shout() {
		// TODO Auto-generated method stub
		System.out.println("汪汪汪!!!");
	}
	
}

class Cat extends Animal{

	@Override
	void eat() {
		// TODO Auto-generated method stub
		System.out.println("cat eat fish");
	}

	@Override
	void shout() {
		// TODO Auto-generated method stub
		System.out.println("喵喵喵!!!");
	}
	
}

abstract class Timer{
	
	void spendTime() {
		long start = System.currentTimeMillis();
		code();
		long end = System.currentTimeMillis();	
		System.out.println("Programe spend time:" + (end - start));
	}
	
	abstract void code();
	
}

class FindPrime extends Timer{

	@Override
	void code() {
		for (int i = 2; i <= 1000; i++) {
			boolean mark = true;
			for (int j = 2; j < Math.sqrt(i); j++) {
				if(i % j == 0) {
					mark = false;
					break;
				}				
			}
			if(mark) {
			System.out.println(i);
			}
		}		
	}	
}

abstract class Employee{
	private int ID;
	private String name;
	private BirthDate birthday;
	abstract double earning();
	public Employee() {
		super();
	}
	public Employee(int iD, String name, BirthDate birthday) {
		super();
		ID = iD;
		this.name = name;
		this.birthday = birthday;
	}
	
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public BirthDate getBirthday() {
		return birthday;
	}
	public void setBirthday(BirthDate birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Employee [ID=" + ID + ", name=" + name + ", birthday=" + this.birthday.toDateString() + "]";
	}
	
		
}

class BirthDate{
	private int day;
	private int month;
	private int year;
	public BirthDate() {
		super();
	}
	public BirthDate(int year, int month, int day) {
		super();
		this.day = day;
		this.month = month;
		this.year = year;
	}	
	
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	String toDateString() {
		return year + "年" + month + "月" + day + "日";
	}	
}

class SalariedEmployee extends Employee{
    private double monthlySalary;
    
	

	public SalariedEmployee() {
		super();
	}

	public SalariedEmployee(int iD, String name, BirthDate birthday,double monthlySalary) {
		super(iD, name, birthday);
		this.monthlySalary = monthlySalary;
	}

	public double getMonthlySalary() {
		return monthlySalary;
	}
	
	public void setMonthlySalary(double monthlySalary) {
		this.monthlySalary = monthlySalary;
	}
	@Override
	double earning() {
		// TODO Auto-generated method stub
		return monthlySalary;
	}

	@Override
	public String toString() {
		return "SalariedEmployee [ " + super.toString() + ", monthlySalary=" + monthlySalary + "]";
	}
	
	
}
class HourlyEmployee extends Employee{
	private double hourlySalary;
	private int hour;
	public HourlyEmployee() {
		super();
	}

	public HourlyEmployee(int iD, String name, BirthDate birthday,double hourlySalary,int hour) {
		super(iD, name, birthday);
		this.hourlySalary = hourlySalary;
		this.hour = hour;
	}
	
	public double getHourlySalary() {
		return hourlySalary;
	}

	public void setHourlySalary(double hourlySalary) {
		this.hourlySalary = hourlySalary;
	}

	@Override
	double earning() {
		// TODO Auto-generated method stub
		return hour * hourlySalary;
	}

	@Override
	public String toString() {
		return "HourlyEmployee [ " + super.toString() + ", hourlySalary=" + hourlySalary + ", hour=" + hour + "]";
	}
		
}
Published 47 original articles · won praise 1 · views 1050

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104396168