抽象类与抽象方法

抽象类与抽象方法

package com.test1.java;

/*
 * abstract可以修饰:类、方法
 * 抽象类:不能实例化(有构造器,子类实例化用)
 * 抽象方法:只有声明,没有方法体
 * 			抽象方法一定存在于抽象类中,抽象类不一定有抽象方法
 * 子类重写父类(包括间接父类)所有抽象方法后,才能进行子类实例化,否则子类也变成抽象类才行
 */

public class AbstractTest {

	public static void main(String[] args) {
	
//		Person p = new Person();//abstract类,不可实例化
//		p.eat();
	}
}

abstract class Person{
	String name;
	int age;
	
	
	public Person() {
		super();
	}


	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public void eat() {
		System.out.println("吃饭");
	}
	//抽象方法
    public abstract void walk();
}

class Student extends Person{
	public Student(String name,int age){
		super(name,age);
	}
	public void walk() {
		
	}
}

抽象类与抽象方法
练习
抽象类与抽象方法

package com.test1.java;

abstract public class Employee {

	private String name;
	private int id;
	private int salary;
	public Employee() {
		super();
	}
	
	public Employee(String name, int id, int salary) {
		super();
		this.name = name;
		this.id = id;
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
	abstract public void work();
}
class Manager extends Employee{
	private double bonus;
	
	public void work() {
		System.out.println("管理");
	}
	

	public Manager(double bonus) {
		super();
		this.bonus = bonus;
	}


	public Manager(String name, int id, int salary, double bonus) {
		super(name, id, salary);
		this.bonus = bonus;
	}


	public double getBonus() {
		return bonus;
	}

	public void setBonus(double bonus) {
		this.bonus = bonus;
	}

}

package com.test1.java;

public class CommonEmployee extends Employee{

	public void work() {
		System.out.println("工作");
	}
}

package com.test1.java;

public class EmployeeTest {

	public static void main(String[] args) {
		//多态
		Employee man = new Manager("张三",1001,5000,2000);
		man.work();
	}
}

匿名子类:大括号里重写抽象方法
抽象类与抽象方法
抽象类与抽象方法
抽象类与抽象方法

练习
抽象类与抽象方法
抽象类与抽象方法

package com.practice.java;
/*
 * 员工信息
 */

abstract public class Employee {
	//员工信息---姓名、编号、生日
	private String name;
	private int number;
	private MyDate birthday;
	
	public abstract double earnings();//薪资
		
	//构造器
	public Employee(String name, int number, MyDate birthday) {
		super();
		this.name = name;
		this.number = number;
		this.birthday = birthday;
	}
	
	
	public String getName() {
		return name;
	}

	public int getNumber() {
		return number;
	}

	public MyDate getBirthday() {
		return birthday;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + ", number=" + number + "birthday=" + birthday +"]";
	}	
}

package com.practice.java;

public class MyDate {

	private int year;
	private int month;
	private int day;
	@Override
	public String toString() {
		return year + "年" + month + "月" + day + "日";
	}
	public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}
	public int getMonth() {
		return month;
	}	
}

package com.practice.java;

//月薪

public class SalariedEmployee extends Employee{

	private double monthlySalary;
	
	public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {
		super(name, number, birthday);
		this.monthlySalary = monthlySalary;
	}
	

//薪资方法重写
	public double earnings() {
		return monthlySalary;
	}


	@Override
	public String toString() {
		return "类型:月薪员工" + " 姓名:" + getName() + " 编号:" + getNumber() + "生日:" + getBirthday().toString();
	}
		
	
}

package com.practice.java;

//时薪

public class HourlyEmployee extends Employee{

	private double wage;
	private int hour;
	
	public HourlyEmployee(String name, int number, MyDate birthday, double wage, int hour) {
		super(name, number, birthday);
		this.wage = wage;
		this.hour = hour;
	}
	
	//薪资方法重写
	public double earnings() {
		return wage*hour;
	}	

	@Override
	public String toString() {
		return "类型:月薪员工" + " 姓名:" + getName() + " 编号:" + getNumber() + "生日:" + getBirthday().toString();
	}
	
}

package com.practice.java;

import java.util.Scanner;

public class PayrollSystem {

	public static void main(String[] args) {
		Employee[] employee = new Employee[5];//new的是数组,不是对象
		
		employee[0] = new SalariedEmployee("张三",1001,new MyDate(1999,10,12),5000);
		employee[1] = new HourlyEmployee("李四",1002,new MyDate(1999,9,23),30,240);
		employee[2] = new SalariedEmployee("王五",1003,new MyDate(1994,10,12),7000);
		employee[3] = new HourlyEmployee("赵六",1004,new MyDate(1995,6,12),50,240);
		employee[4] = new SalariedEmployee("王贵",1005,new MyDate(2000,7,12),5000);
		
		for(int i = 0;i < employee.length;i++) {
			System.out.println(employee[i]);
			System.out.println("工资:" + employee[i].earnings());
		}
		
		boolean isFlag = true;
		while(isFlag) {
			int sum = 0;
			System.out.println("请输入本月月份:");
			Scanner scan = new Scanner(System.in);
			int month = scan.nextInt();
			if(month > 0 && month < 13 ) {
				for(int i = 0;i < employee.length;i++) {
					if(employee[i].getBirthday().getMonth() == month) {
						sum++;
						System.out.println(employee[i] + "生日福利100元");
					}
				}
			}else{
				System.out.println("请重新输入");
				continue;
			}
			if(sum == 0) {
				System.out.println("没有员工过生日");
			}
			isFlag = false;
		}
		
	}
	
	
}

上一篇:RestTemplate基本使用


下一篇:Java数组--数组的概述