#千锋逆战班#
在千锋“逆战”学习第 18 天,
今天学习的是接口的相关内容。
中国加油!武汉加油!千锋加油!
学习的脚步不停止!
天道酬勤,继续加油!
Day 18 作业
package com.day18.TestWork;
public class TestWork {
public static void main(String args[]) {
IC ic = new ClassE();
ClassE ic1 = (ClassE) ic;
ic1.ma();
ic1.mb();
ic1.mc();
ic1.md();
System.out.println(ic1 instanceof IA);
System.out.println(ic1 instanceof IB);
System.out.println(ic1 instanceof IC);
System.out.println(ic1 instanceof ID);
System.out.println(ic1 instanceof ClassE);
}
}
interface IA{
void ma();
}
interface IB extends IA{
void mb();
}
interface IC{
void mc();
}
interface ID extends IB,IC{
void md();
}
class ClassE implements ID{
@Override
public void mb() {
// TODO Auto-generated method stub
}
@Override
public void ma() {
// TODO Auto-generated method stub
}
@Override
public void mc() {
// TODO Auto-generated method stub
}
@Override
public void md() {
// TODO Auto-generated method stub
}
}
-
true
true
true
true -
A.C.D.E
Red Light shine in Red
Red Light shine in Yellow
Red Light shine in Green
TeacherA teach Java
TeacherB teach Java
as[0].eat();
as[1].eat();
as[2].eat();
((Dog) as[0]).play();
((Cat) as[1]).play();
package com.day18.TestWork;
public class TestEmployee {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] e = new Employee[] {
new SalarieEmployee("Tom", 12, 10000),
new HourlyEmployee("Mike", 1, 200, 20),
new SalesEmployee("Mary", 8, 5000, 0.45),
new BasePlusSalesEmployee("Jane", 11, 5000, 0.45 , 5000)
};
for(int i=0;i<e.length;i++) {
System.out.println(e[i].getSalary(4));
}
printOvaetimeSalary();
}
public static void printOvaetimeSalary(){
double sums = (double)SalesEmployee.value * 2000 + (double)BasePlusSalesEmployee.nums * 1000;
System.out.println("加班费总和为:" + sums);
}
}
interface OvertimePay {
double salesEmployee = 2000.0;
double basePlusSalesEmployee = 1000;
}
class Employee{
private String name;
private int month;
public Employee() {}
public Employee(String name,int month) {
this.name=name;
this.month=month;
}
public double getSalary(int month) {
if(this.month==month)
return 100;
return 0.0;
}
}
class SalarieEmployee extends Employee{
private double salary;
public SalarieEmployee() {}
public SalarieEmployee(String name,int month,double salary) {
super(name,month);
this.salary = salary;
}
public double getSalary(int month) {
return salary+super.getSalary(month);
}
}
class HourlyEmployee extends Employee{
private double hourlySalary;
private int hours;
public HourlyEmployee() {}
public HourlyEmployee(String name,int month,double hourlySalary, int hours) {
super(name,month);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getSalary(int month) {
if (hours > 160){
return hourlySalary * 160 + 1.5 * hourlySalary * (hours - 160) + super.getSalary(month);
}
return hourlySalary * hours + super.getSalary(month);
}
}
class SalesEmployee extends Employee implements OvertimePay{
private double sales;
private double rate;
public static int value;
public SalesEmployee() {
value++;
}
public SalesEmployee(String name,int month,double sales, double rate) {
super(name,month);
this.sales = sales;
this.rate = rate;
value++;
}
public double getSalary(int month) {
return sales * rate + super.getSalary(month)+salesEmployee;
}
}
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary;
public static int nums=0;
public BasePlusSalesEmployee() {
nums++;
}
public BasePlusSalesEmployee(String name,int month,double sales, double rate, double baseSalary) {
super(name,month,sales, rate);
this.baseSalary = baseSalary;
nums++;
}
public double getSalary(int month) {
return baseSalary + super.getSalary(month)+basePlusSalesEmployee;
}
}
-
第一种情况需要实现接口后,对接口内所有的抽象方法进行覆盖,
第二种是通过一个父类覆盖接口的方法,再以子类对父类进行继承。
package com.day18.TestWork;
import java.util.Scanner;
public class Goldbach {
public static void main(String[] args) {
Goldbach gd = new Goldbach();
Scanner scanner=new Scanner(System.in);
System.out.println("输入一个大于6的偶数:");
int n=scanner.nextInt();
gd.goldBach(n);
}
//判断传入的数n是否是大于6的偶数
void goldBach(int n){
if (n%2 == 0 && n>6) {
//判断两个加数是否同为质数
for (int i = 2; i <= n/2; i++) {
if (Prime(i) && Prime(n-i)) {
System.out.println("该数为: " + i + "与" + (n-i) + "的和" );
}
}
}else {
System.out.println("输入错误!");
}
}
//判断数是否是质数,只能整除1和本身
boolean Prime(int n){
for (int i = 2; i < n; i++) {
if (n%i == 0) {
return false;
}
}
return true;
}
}