千锋逆战班学习第18天

感觉每天学习的知识越来越难,但不会放弃

3.

L

Class E{

public void ma(){}

public void mb(){}

public void mc(){} 

public void md(){}

}

Ll

E mye = (E)ic;

mye.ma();  

mye.mb();        

mye.mc();          

mye.md();

lll  false  false   true   false  true

4.true false   true true

5.ABCDE

6.

Red Ligth shine in Red

YellowLigth shine in Yellow

Green Ligth shine in Green

7   TeacherA teach java

TeacherB teach java

8  

for(int i = 0 ; i < as.length;i++){

System.out.println(as[i].eat)

}

for(int i = 0 ; i < as.length;i++){

If(as[i] instanceof Pet){

System.out.println(as[i].play)

}

}

9.

public class TestEmployee {

public static void main(String[] args) {

Employee[] em = new Employee[4];

SalariedEmployee work1 = new SalariedEmployee("tom" , 5 , 3000.0);

HourlyEmployee work2 = new HourlyEmployee("jack" , 3 , 20.0 , 200);

SalesEmployee work3 = new SalesEmployee("marry" , 6 , 0.1 , 50000);

BasePlusSalesEmployee work4 = new BasePlusSalesEmployee("anni" , 10 , 40000 , 0.05 , 1000.0 );

Months mon = new Months();

mon.months(work1);

mon.months(work2);

mon.months(work3);

mon.months(work4);

 

em[0] = work1;

em[1] = work2;

em[2] = work3;

em[3] = work4;

for(int i = 0;i < em.length;i++){

System.out.println("第三月的工资为:" + em[i].getName() +"   " + em[i].getSalary(3));

}

printOvaetimeSalary();

}

public static void printOvaetimeSalary(){

double sums = SalariedEmployee.value * 2000 + HourlyEmployee.nums * 1000;

System.out.println("加班费总和为:" + sums);

}

}

class Months{//计算工资

public double months(Employee em){

double salary = em.getSalary(3);

return salary;

}

}

abstract class Employee{//-------------------------------父类

private String name;

private int birthdayMonth;

public abstract double getSalary(int month);

public double giveMonth(int month){

if(month == this.birthdayMonth){

return 100D;

}

return 0D;

}

public Employee() {}

public Employee(String name, int birthdayMonth) {

super();

this.name = name;

this.birthdayMonth = birthdayMonth;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getBirthdayMonth() {

return birthdayMonth;

}

public void setBirthdayMonth(int birthdayMonth) {

this.birthdayMonth = birthdayMonth;

}

}

class SalariedEmployee extends Employee implements OvertimePay{//---------------------固定工资

private double salary;

public static int value = 0;

public double getSalary(int month){

return salary + giveMonth(month) + salesEmployee;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

 

public SalariedEmployee() {

value++;

}

public SalariedEmployee(String name, int birthdayMonth , double salary) {

super();

value++;

this.salary = salary;

super.setName(name);

super.setBirthdayMonth(birthdayMonth);

}

}

class HourlyEmployee extends Employee implements OvertimePay{//------------------------------小时工资

private double hourlySalary;

private int hours;

public static int nums = 0;

public double getSalary(int month){

if(this.hours > 160){

return (this.hourlySalary * 160) + ((this.hours - 160) * 1.5 * this.hourlySalary) + giveMonth(month) + basePlusSalesEmployee;

}

return hours * hourlySalary + giveMonth(month) + basePlusSalesEmployee;

}

public HourlyEmployee(){

nums++;

}

public HourlyEmployee(String name, int birthdayMonth , double hourlySalary, int hours) {

super();

nums++;

this.hourlySalary = hourlySalary;

this.hours = hours;

super.setName(name);

super.setBirthdayMonth(birthdayMonth);

}

public double getHourlySalary() {

return hourlySalary;

}

public void setHourlySalary(double hourlySalary) {

this.hourlySalary = hourlySalary;

}

public int getHours() {

return hours;

}

public void setHours(int hours) {

this.hours = hours;

}

}

class SalesEmployee extends Employee{//--------------------------------提成工资

private double sales;

private double rate;

public SalesEmployee(){}

public double getSalary(int month){

return sales * rate + giveMonth(month);

}

public SalesEmployee(String name, int birthdayMonth , double sales, double rate) {

super();

this.sales = sales;

this.rate = rate;

super.setName(name);

super.setBirthdayMonth(birthdayMonth);

}

public double getSales() {

return sales;

}

public void setSales(double sales) {

this.sales = sales;

}

public double getRate() {

return rate;

}

public void setRate(double rate) {

this.rate = rate;

}

}

class BasePlusSalesEmployee extends SalesEmployee{//-----------------------------固定工资加提成工资

private double baseSalary;

public double getSalary(int month){

return this.getRate() * this.getSales() + baseSalary + giveMonth(month);

}

public BasePlusSalesEmployee(){}

public BasePlusSalesEmployee(String name, int birthdayMonth,double sales, double rate, double baseSalary) {

this.baseSalary = baseSalary;

super.setRate(rate);

super.setSales(sales);

super.setName(name);

super.setBirthdayMonth(birthdayMonth);

}

public double getBaseSalary() {

return baseSalary;

}

public void setBaseSalary(double baseSalary) {

this.baseSalary = baseSalary;

}

}

接口

interface OvertimePay {

double salesEmployee = 2000.0;

double basePlusSalesEmployee = 1000;

}

10.

一个是实现接口里的抽象方法,一个是继承父类中的方法

作用: 作为父类为子类传承方法,可以作为引用实现多态,可以作为实现类实现接口中的抽象方法

11.

import java.util.Scanner;

interface MathTool{

boolean isPrime(int n);

}

//接口实现者

class MathToolImpl implements MathTool{

public boolean isPrime(int n) {

for(int i = 2; i<= Math.sqrt(n); i++){

if (n % i == 0) return false;

}

return true;

}

}

//接口的使用者

public class TestGoldBach {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

MathTool mt = new MathToolImpl();

for(int i = 2; i<=n/2; i++){

if (mt.isPrime(i) && mt.isPrime(n - i)){

System.out.println(n + "=" + i + "+" + (n - i));

}

}

}

}

发布了24 篇原创文章 · 获赞 69 · 访问量 1187

猜你喜欢

转载自blog.csdn.net/S9264L/article/details/104527034