接口

接口继承关系:
1、多继承: extends 接口1,接口2…
2、多实现: implements 接口1,接口2…
接口常量:
将多个表示状态或固定值的常量,定义在一个接口内,提高代码可读性。
接口宏观概念:
接口是一种标准,用于降低耦合度,耦合度越低越松散。
接口回调:
先有接口调用者,后有接口发现者。

以下为作业:
3、
public class Interface {
public static void main(String[] args) {
IC ic = new ClassE();
ClassE ce = new ClassE();
ce.ma();
ce.mb();
ce.mc();
ce.md();
System.out.println(ic instanceof IA);
System.out.println(ic instanceof IB);
System.out.println(ic instanceof IC);
System.out.println(ic instanceof ID);
System.out.println(ic 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{ //实现ID
public void ma() {}
public void mb() {}
public void mc() {}
public void md() {}
}

4、
public class TestMain {
public static void main(String[] args) {
MySuper ms = new MySub();
System.out.println(ms instanceof IA);
System.out.println(ms instanceof IB);
System.out.println(ms instanceof MySuper);
System.out.println(ms instanceof MySub);
}
}
interface IA{
void ma();
}
interface IB{
void mb();
}
class MySuper implements IA{
public void ma() {}
}
class MySub extends MySuper implements IB{
public void mb() {}
}

5、
public class TestLamp {
public static void main(String[] args) {
Light[] ls = new Light[3];
ls[0] = new RedLight();
ls[1] = new YellowLight();
ls[2] = new GreedLight();
Lamp lamp = new Lamp();
for(int i = 0 ; i < ls.length ; i++) {
lamp.setLight(ls[i]);
lamp.on();
}

}

}
interface Light{
void shine();
}
class RedLight implements Light{
public void shine(){
System.out.println(“Red Light shine in Red”);
}
}
class YellowLight implements Light{
public void shine(){
System.out.println(“Yellow Light shine in Yellow”);
}
}
class GreedLight implements Light{
public void shine(){
System.out.println(“Green Light shine in Green”);
}
}
class Lamp{
private Light light;
public void setLight(Light light) {
this.light = light;
}
public void on() {
light.shine();
}
}

7、
public class TestShool {
public static void main(String[] args) {
JavaTeacher jt = Shool.getTeacher(0);
jt.teach();
jt = Shool.getTeacher(10);
jt.teach();
}
}
interface JavaTeacher{
void teach();
}
class TeacherA implements JavaTeacher{
public void teach() {
System.out.println(“TeacherA teach Java”);
}
}
class TeacherB implements JavaTeacher{
public void teach() {
System.out.println(“TeacherB teach Java”);
}
}
class Shool{
public static JavaTeacher getTeacher(int i) {
if(i == 0) {
return new TeacherA();
}else {
return new TeacherB();
}
}
}

8、
public class TestMain {
public static void main(String[] args) {
Animal[] as = new Animal[3];
as[0] = new Dog();
as[1] = new Cat();
as[2] = new Wolf();
for(int i = 0 ; i < as.length ; i++) {
as[i].eat();
}
((Dog)as[0]).play();
((Cat)as[1]).play();
}
}
abstract class Animal{
public abstract void eat();

}
interface Pet{
void play();
}
class Dog extends Animal implements Pet{
public void eat() {
System.out.println(“Dog eat Bones”);
}
public void play() {
System.out.println(“Play with Dog”);
}
}
class Cat extends Animal implements Pet{
public void eat() {
System.out.println(“Cat eat fish”);
}
public void play() {
System.out.println(“Play with Cat”);
}
}
class Wolf extends Animal{
public void eat() {
System.out.println(“Wolf eat meat”);
}
}

9、
public class Testmoeny {

public static void main(String[] args) {
	Employee[] empls = new Employee[] {
		new SalariedEmployee("zs" , 1 , 8000.0),
		new HourlyEmployee("ls" , 1 , 20.5 , 180),
		new SalesEmployee("ww" , 2 , 2000.4 , 0.1),
		new BasePlueSalesEmployee("lw" , 5 , 8000.2 , 0.1 , 3000.5)
	};
	

	for(int i = 0 ; i < empls.length ; i++) {
		if(empls[i] instanceof SalariedEmployee) {
			SalariedEmployee sla = (SalariedEmployee)empls[i];
			System.out.println(sla.everyMonth(3));
		}else if(empls[i] instanceof HourlyEmployee) {
			HourlyEmployee hours = (HourlyEmployee)empls[i];
			System.out.println(hours.hourMoney(6));
		}else if(empls[i] instanceof SalesEmployee) {
			SalesEmployee sas = (SalesEmployee)empls[i];
			if(sas instanceof BasePlueSalesEmployee) {
				BasePlueSalesEmployee bas = (BasePlueSalesEmployee)sas;
				System.out.println(bas.sales(8));
			}else {
				System.out.println(sas.sales(10));
			}
		}		
    }
}

}

interface Bonus{
double bs();
}

class Employee{
private String name;
private int birMonth;
public Employee() {}
public Employee(String name , int birMonth) {
this.name = name;
this.birMonth = birMonth;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name = name;
}
public void setBirMonth(int birMonth) {
this.birMonth = birMonth;
}
public int getBirMonth() {
return this.birMonth = birMonth;
}
double douMoney = 0;
public void getSalary(int month) {
int bir = getBirMonth();
if(month == bir || month%12 == bir) {
douMoney += 100;
}
}
}

class SalariedEmployee extends Employee implements Bonus{
private double everyMoney;
public SalariedEmployee() {}
public SalariedEmployee(String name , int birMonth , double everyMoney){
super(name , birMonth);
this.everyMoney = everyMoney;
}
public double getEveryMoney() {
return this.everyMoney = everyMoney;
}
public void setEveryMoney(double everyMoney) {
this.everyMoney = everyMoney;
}
public double bs() {
double bonus = 2000;
return bonus;
}
public double everyMonth(int month) {
double bMoney = bs()*(month - getBirMonth());
if(month > getBirMonth()) {
bMoney += (month - getBirMonth())*everyMoney + douMoney;
return bMoney ;
}else return 0;

}

}
class HourlyEmployee extends Employee{
private double hourMoney;
private double hour;
HourlyEmployee(){}
public HourlyEmployee(String name , int birMonth , double hourMoney , double hour) {
super(name , birMonth);
this.hourMoney = hourMoney;
this.hour = hour;
}
public double getHourMoney() {
return hourMoney;
}
public double getHour() {
return hour;
}
public void setHourMoney(double hourMoney) {
this.hourMoney = hourMoney;
}
public void setHour(double hour) {
this.hour = hour;
}
public double hourMoney(int month) {

	double money = 0;
	if(getHour() > 160) {
		money = ((getHour() - 160)*1.5 + 160)*getHourMoney()*(month - getBirMonth()) + douMoney;
	}else {
		money = getHourMoney()*getHour()*(month - getBirMonth());
	}
	return money;
}

}
class SalesEmployee extends Employee{
private double sale;
private double lift;
SalesEmployee(){}
public SalesEmployee(String name , int birMonth , double sale , double lift) {
super(name , birMonth);
this.sale = sale;
this.lift = lift;
}
public void setSale(double sale) {
this.sale = sale;
}
public double getSale() {
return this.sale = sale;}
public void setLift(double lift) {this.lift = lift;}
public double getLift() {return this.lift = lift;}

public double sales(int month) {
	double salesMoney = 0;
	for(int i = 0 ; i < (month - getBirMonth()) ; i++) {
		salesMoney += getSale()*getLift() + douMoney;
	}
	
	return salesMoney;
}

}
class BasePlueSalesEmployee extends SalesEmployee{
private double baseMoney;
BasePlueSalesEmployee(){}
public BasePlueSalesEmployee(String name , int birMonth , double sale , double lift , double baseMoney) {
super(name , birMonth , sale , lift);
this.baseMoney = baseMoney;
}
public void setBaseMoney(double baseMoney) {
this.baseMoney = baseMoney;
}
public double getBaseMoney() {
return this.baseMoney = baseMoney;
}
public double bs() {
double d = 1000;
return d;
}
public double base(int month) {
double baseMoney = (getBaseMoney()+bs())*month + sales(month) + douMoney;
return baseMoney;
}

}

10、歌德猜想
public class GDBH {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
GeDe ge = new GeDe();
ge.pri(t);
}
}
public interface Sum{
void pri(int i);
}
public class GeDe implements Sum{
public void pri(int i) {
if(i > 5 && i%2 == 0) {
for(int j = 5 ; j < i ; j++) {
for(int k = 5 ; k <= i-j ; k++) {
if(k%2 != 0 && (j%2 !=0) && (i == (k+j))) {
System.out.println(i +"="+j+"+"+k);
}
}
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值