ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

抽象类与抽象方法

2022-02-22 12:30:03  阅读:126  来源: 互联网

标签:return name int 抽象 new 抽象类 方法 public String


在这里插入图片描述

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;
		}
		
	}
	
	
}

标签:return,name,int,抽象,new,抽象类,方法,public,String
来源: https://blog.csdn.net/qq_41571545/article/details/123051504

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有