2.26Java学习内容:接口

3.(1) void ma();
void mb();
void mc();
void md();
(2)((ClassE).ic).ma();
((ClassE).ic).mb();
ic.mc();
((ClassE).ic).md();
(3)true true true true true
4. true true true true
5. A C D E
6. Red Light shine in Red
Yellow Light shine in Yellow
Green Light 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){
pet pet=pet(as[i]);
System.out.println(pet.play);
}
}
9.
源代码:
package day15.t2;

public class TestQ {

public static void main(String[] args) {
	
	SalariedEmployee e=new SalariedEmployee();
	e.setMonth(10);
	e.setName("tom");
	e.setSalary(5000.0);
	
	
	System.out.println(e.getSalary(10));
	
	HourlyEmployee e1=new HourlyEmployee();
	e1.setName("jack");
	e1.setMonth(9);
	e1.setHours(180);
	e1.setHourlySalary(100.0);
	
	System.out.println(e1.getSalary(10));
	
	SalesEmployee e2 =new SalesEmployee();
	e2.setName("mike");
	e2.setMonth(8);
	e2.setSales(10000.0);
	e2.setRate(0.8);
	
	System.out.println(e2.getSalary(10));
	
	BasePlusSalesEmployee e3=new BasePlusSalesEmployee();
	e3.setName("annie");
	e3.setMonth(7);
	e3.setBaseSalary(6000.0);
	e3.setRate(0.8);
	e3.setSales(10000.0);
	
	System.out.println(e3.getSalary(10));
	
	System.out.println("---------------------------");
	
	e3.method1();
	

	

}

}

class Employee{
private String name;
private int month;

public Employee(){}


public void setName(String name){
	this.name=name;
}
public String getName(){
	return this.name;
}
public void setMonth(int month){
	this.month=month;
}
public int getMonth(){
	return this.month;
}

public double getSalary(int month){
	if(month==getMonth()){
		return 100.0;
	}else{
		return 0.0;
	}
	
}

}

class SalariedEmployee extends Employee{
private double salary;

public SalariedEmployee(){}

public void setSalary(double salary){
	this.salary=salary;
}
public double getSalary(int month){
	
	return this.salary+super.getSalary(month);
}

}

class HourlyEmployee extends Employee{
private double hourlySalary;
private int hours;

public HourlyEmployee(){}
public void setHours(int hours){
	this.hours=hours;
}
public int getHours(){
	return this.hours;
}
public void setHourlySalary(double hourlySalary){
	this.hourlySalary=hourlySalary;
}
public double getHourlySalary(){
	return this.hourlySalary;
}

public double getSalary(int month){
	
	double a;
	if(hours>160){
		a=160*hourlySalary+1.5*hourlySalary*(hours-160);
	}else{
		a=hours*hourlySalary;
	}
	return a+super.getSalary(month);
}

}

class SalesEmployee extends Employee implements plus{
private double sales;
private double rate;
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;
}
public double getSalary(int month){

	double a=sales*rate;
	
	return a+super.getSalary(month)+Sales;
}

}

class BasePlusSalesEmployee extends SalesEmployee implements plus{

private double baseSalary;

public void setBaseSalary(double baseSalary){
	this.baseSalary=baseSalary;
}

public double getSalary(int month){
	
	double a=this.baseSalary;
	return a+super.getSalary(month)+BasicSales;
	
}
public void method1(){
	int pluss=Sales+BasicSales;
	System.out.println("本月发放加班费为"+pluss);
}

}
interface plus{
public static int Sales =2000;
public static int BasicSales=1000;
}
运行结果:
在这里插入图片描述
10
第一种,实现ServiceInterface接口:
这种方法要求我们必须在MyService 类中实现接口中的三个公开抽象方法。
第二种,继承AbstractService:
使用这种方法 我们在MyService中直接继承到了 父类的三种方法,无需再实现这三种方法
当MyService只需要一种方法时,可以通过继承AbstracrService类,然后重写自己需要的方法即可,可以省略不需要的方法,也不用进行覆盖。
AbstracrService类为子类提供了选择性,可以选择自己需要的方法进行覆盖。

AbstractService类在自己的类中实现了接口的三种公开抽象方法,当有子类继承它时,可以直接继承到这三种方法。

  1. 哥德巴赫猜想
    源代码:
    package day18.t1;

public class TestThink {

public static void main(String[] args) {
	System.out.println(method(8));
	System.out.println(method(11));
	System.out.println("------------");
	
	method1(64);

}

public static boolean method(int nums){
	for(int i =2; i<nums;i++){
		if(nums%i==0){
			return false;
		}
	}
	return true;
}

public static void method1(int num) {

	for (int a = 2; a <= num / 2; a++) {
		int b = num - a;
		if (method(a) && method(b)) {
			System.out.println(num + "=" + a + "+" + b);
		}
	}

}

}
运行效果图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值