//父类.并且使用用接口
package cn.yjlblog.wwww;
public interface Compute {
int computer(int n,int m);//抽象方法,胜率abstract ,public
}
//子类add 实现加法运算
package cn.yjlblog.wwww;
public class add implements Compute{
@Override //接口的实现类和抽象类的子类是一样的,要想可以使用new 一个对象,就必须对“抽象类”里的方法
//进行重写
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n+m;
}
}
package cn.yjlblog.wwww;
//子类subtract 实现减法运算
public class subtract implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n-m;
}
}
子类multiplied 实现乘法运算
package cn.yjlblog.wwww;
public class multiplied implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n*m;
}
}
package cn.yjlblog.wwww;
//子类divided 实现整除运算
public class divided implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n/m;
}
}
//应用类UseComepute 用来
package cn.yjlblog.wwww;
public class UseComepute {
public void useCom(Compute com,int one,int two){
int x = com.computer(one, two);
System.out.println("运算结果为:"+x);
}
}
//测试类Test
package cn.yjlblog.wwww;
public class Test {
public static void main(String[] args) {
UseComepute uc = new UseComepute();//应用类生成对象,使用useCome 方法
int one = 10;
int two = 20;
Compute ad = new add();//接口的多态
Compute sub = new subtract();
Compute mul= new multiplied();
Compute div = new divided();
uc.useCom(ad, one, two);
uc.useCom(sub, one, two);
uc.useCom(mul, one, two);
uc.useCom(div, one, two);
//哈哈,类的名字忘记大写了
}
}
public abstract void Printer() {
public void open() {
system.out.print("打印机开启");
}
public void close() {
system.out.print("打印机关闭");
}
abstract void print(); //把print方法定义为抽象方法
}
public HPPrinter extends Printer {
void print(){ //必须重写抽象方法print()
system.out.print("惠普打印机开始打印");
};
}
public CanonPrinter extends Printer {
void print(){ //必须重写抽象方法print()
system.out.print("佳能打印机开始打印");
};
}
public abstract class People { //关键词abstract,声明该类为抽象类
void Num();
abstract void Name(); //声明该方法为抽象方法
}
Interface Person {
void Num();
void Name();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有