Java——16——抽象类与抽象方法

一:抽象类是什么

在这里插入图片描述

二:关键字abstract的使用

1.修饰类,抽象类

在这里插入图片描述

2.修饰方法,抽象方法

在这里插入图片描述

package com.xx.study1103;

public class AbstractTest {
	public static void main(String[] args) {
		/*
		 * 报错,一旦Person类被抽象了,就不可以实例化,也就是不可造对象 Person p1=new Person(); p1.eat();
		 */
	}
}

//父类
abstract class Person {
	String name;
	int age;

	// 只要是类就一定有构造器,虽然是抽象类不能自己调用构造器
	// 但是子类可以调用
	public Person() {
	}

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	// 抽象方法
	public abstract void eat();

	public void walk() {
		System.out.println("我在走路");
	}

}

abstract class Student extends Person {
	public Student(String name, int age) {
		super(name, age);
	}
	/*
	public void eat() {
		System.out.println("重写方法");
	}
	*/
}

3.应用场景

4.abstract使用上的注意点

在这里插入图片描述

三:抽象性的练习

在这里插入图片描述

Employee.java

package com.xx.study1103;

public abstract class Employee {
    private String name;
    private int id;
    private double salary;
    
	public Employee() {}
	public Employee(String name,int id,double salary) {
		this.name=name;
		this.id=id;
		this.salary=salary;
	}
	//每个人工作内容不同
	 public abstract void  work();
}

Manager.java

package com.xx.study1103;

public class Manager extends Employee {
  private double bonus;
  
	@Override
	public void work() {
		// TODO Auto-generated method stub
		System.out.println("管理员工");
	}
	//alt+shift+s 构造器  最上面下拉框选择父类带参数的

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

	
    

	

}

CommonEmployee.java

package com.xx.study1103;

public class CommonEmployee extends Employee {

	@Override
	public void work() {
		// TODO Auto-generated method stub
		System.out.println("工人");
	}
   
}

EmployeeTest.java

package com.xx.study1103;

public class EmployeeTest {
  public static void main(String[] args) {
  Manager manager=new Manager("丑人", 1001, 5000, 50000); 
  manager.work();
  CommonEmployee commonEmployee=new CommonEmployee();
  commonEmployee.work();
}
}

四:模板方法设计模式

在这里插入图片描述

package com.xx.study1103;

public class TemplayeTest {
	public static void main(String[] args) {
		SubTemplate t=new SubTemplate();
		t.spendTime();
	}
	
}
abstract class Template{
	public void spendTime() {
		long start =System.currentTimeMillis();
	    code();//不确定的部分,易变的部分
		long end=System.currentTimeMillis();
		System.out.println(end-start);//执行这段代码花费的时间
	}
	public abstract void code();
}
class SubTemplate extends Template{

	@Override
	public void code() {
		// TODO Auto-generated method stub
		for(int i=2;i<=100;i++) {
			boolean isFlag=true;
			for(int j=2;j<Math.sqrt(i);j++) {
				if(i%j==0) {
					isFlag=false;
					break;
				}
			}
			if(isFlag) {
				System.out.println(i);
			}
		}
	}
	
}

五:抽象类的练习

在这里插入图片描述在这里插入图片描述
Employee.java

package com.xx.study1103;

public abstract class Employee {
    private String name;
    private int number;
    private MyDate birthday;
    
	public Employee() {}
	public Employee(String name,int id,MyDate birthday) {
		this.name=name;
		this.number=number;
		this.birthday=birthday;
	}
	
	 public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public MyDate getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}
	public abstract double  earnings();
	@Override
	public String toString() {
		//birthday是自定义类  否则输出的是地址值
		return "[name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() + "]";
	}
	
	 
}

MyDate.java

package com.xx.study1103;

public class MyDate {
	private int year;
	private int month;
	private int day;

	public MyDate() {
		super();
	}

	public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
    public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String toDateString() {
    	return year+"年"+month+"月"+day+"日";
    }
}

SalariedEmployee.java

package com.xx.study1103;

public class SalariedEmployee extends Employee{
	private double monthlySalary;
    public SalariedEmployee(String name,int number,MyDate birthday) {
    	super(name,number,birthday);
    }
    // 重载构造器
    public SalariedEmployee(String name,int number,MyDate birthday,double monthlySalary) {
    	super(name,number,birthday);
    	this.monthlySalary=monthlySalary;
    }
    
	public double getMonthlySalary() {
		return monthlySalary;
	}

	public void setMonthlySalary(double monthlySalary) {
		this.monthlySalary = monthlySalary;
	}

	@Override
	public double earnings() {
		// TODO Auto-generated method stub
		return monthlySalary;
	}
	//SalariedEmployee表明对象类型
    public String toString() {
    	return "SalariedEmployee["+super.toString()+"]";
    }
}

HourlyEmployee.java

package com.xx.study1103;

public class HourlyEmployee extends Employee {
	private int wage;// 每小时的工资
	private int hour;// 月工作的小时数

	public HourlyEmployee(String name,int number,MyDate birthday,int wage, int hour) {
		super(name,number,birthday);
		this.wage = wage;
		this.hour = hour;
	}

	@Override
	public double earnings() {
		// TODO Auto-generated method stub
		return wage*hour;
	}

	@Override
	public String toString() {
		//父类的toString方法
		return "HourlyEmployee["+super.toString()+"]";
				
	}

	
   
}

PayrollSystem.java

package com.xx.study1103;

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

public class PayrollSystem {
  public static void main(String[] args) {
	  //方式一
	  Scanner scanner=new Scanner(System.in);
	  System.out.println("请输入当月的月份");
	  int month=scanner.nextInt();
	  //方式二
	  /*
	  Calendar calendar=Calendar.getInstance();
	  int month=calendar.get(Calendar.MONTH);//获取当前的月份
	  System.out.println(month);//1月份是0 如果按照方拾贰这样写最下面的if判断要加1
	  */
	  Employee[] emps=new Employee[2];
	  emps[0]=new SalariedEmployee("马云",1001,new MyDate(1979,5,3),100000);
	  emps[1]=new HourlyEmployee("马蜂",1002,new MyDate(1998,6,25),50,240);
	  for(int i=0;i<emps.length;i++) {
		  System.out.println(emps[i]);
		  double salary=emps[i].earnings();
		  System.out.println("月工资为:"+salary);
		  if(month==emps[i].getBirthday().getMonth()) {
			  System.out.println("生日快乐,奖励100元!");
		  }
	  }
	 
	   
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值