题目要求:
设计一个抽象类,声明有关算术运算的方法,并创建四个(或者一个)子类继承该抽象的类,完成进行加、减、乘、除四则运算。
整个题目总共经过了三轮完善,第一发没有利用抽象类,只是简单的实现了基本操作;第二枚用了四个子类去继承这个原有的抽象类,其中就用到了可以骗人的适配器模式,没有实现方法的重写,而是用的方法的重载;第三弹则是用一个子类去继承该抽象类,并重写了其中的抽象方法完成了运算。建议大家直接看第三种!
JAVA中的适配器模式:在学习抽象类的继承和接口的实现时,我们总是要全部重写其中的抽象方法,能不能不这么烦呢,有一种适配器模式,可以巧妙的使用,来达到这个目的。
Aa.java
package cn.nefu.edu.zhihu;
public interface Aa {
void a();
void b();
}
Ab.java
package cn.nefu.edu.zhihu;
public class Ab implements Aa{
public void a() {
// TODO Auto-generated method stub
}
public void b() {
// TODO Auto-generated method stub
}
}
Ac.java
package cn.nefu.edu.zhihu;
public class Ac extends Ab{
}
方法一代码:
Youlishu.java
package cn.nefu.edu.zhihu;
public class Youlishu {
int fenzi,fenmu;
Youlishu(){
}
Youlishu(int fenzi,int fenmu){
this.fenzi=fenzi;
this.fenmu=fenmu;
}
int gcd(int x,int y){
if(y==0)
return x;
else
return gcd(y,x%y);
}
int lcm(int x,int y){
return x*y/gcd(x,y);
}
Youlishu add(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
int d1,d2;
int lcmm=lcm(a.fenmu,b.fenmu);
c.fenmu=lcmm;
d1=lcmm/a.fenmu;
d2=lcmm/b.fenmu;
c.fenzi=d1*a.fenzi+d2*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
Youlishu sub(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
int d1,d2;
int lcmm=lcm(a.fenmu,b.fenmu);
c.fenmu=lcmm;
d1=lcmm/a.fenmu;
d2=lcmm/b.fenmu;
c.fenzi=d1*a.fenzi-d2*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
Youlishu cheng(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
c.fenzi=a.fenzi*b.fenzi;
c.fenmu=a.fenmu*b.fenmu;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
Youlishu chu(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
c.fenzi=a.fenzi*b.fenmu;
c.fenmu=a.fenmu*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
//getxiaoshu()
void show(){
System.out.println(fenzi+"/"+fenmu);
}
}
Test1.java
package cn.nefu.edu.zhihu;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Youlishu a=new Youlishu(2,3);
Youlishu b=new Youlishu(3,2);
Youlishu c=new Youlishu(0,0);
c=c.add(a,b);
c.show();
c=c.sub(a,b);
c.show();
c=c.cheng(a,b);
c.show();
c=c.chu(a,b);
c.show();
System.out.println("Please Input Youlishu a and b:");
Scanner reader=new Scanner(System.in);
a.fenzi=reader.nextInt();
a.fenmu=reader.nextInt();
b.fenzi=reader.nextInt();
b.fenmu=reader.nextInt();
c=c.add(a,b);
c.show();
c=c.sub(a,b);
c.show();
c=c.cheng(a,b);
c.show();
c=c.chu(a,b);
c.show();
}
}
Yunsuan.java
package cn.edu.nefu.zhihu;
public abstract class Yunsuan {
int gcd(int a,int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
int lcm(int a,int b){
return a*b/gcd(a,b);
}
abstract Object add(Object a,Object b);
abstract Object sub(Object a,Object b);
abstract Object multi(Object a,Object b);
abstract Object divid(Object a,Object b);
}
Yunsuanzi.java
package cn.edu.nefu.zhihu; //适配器模式
public class Yunsuanzi extends Yunsuan{
@Override
Object add(Object a, Object b) {
// TODO Auto-generated method stub
return null;
}
@Override
Object divid(Object a, Object b) {
// TODO Auto-generated method stub
return null;
}
@Override
Object multi(Object a, Object b) {
// TODO Auto-generated method stub
return null;
}
@Override
Object sub(Object a, Object b) {
// TODO Auto-generated method stub
return null;
}
}
Add.java
package cn.edu.nefu.zhihu;
public class Add extends Yunsuanzi {
Object add(Youlishu a,Youlishu b){
Youlishu c = new Youlishu(0,0);
int d1,d2;
int lcmm=lcm(a.fenmu,b.fenmu);
c.fenmu=lcmm;
d1=lcmm/a.fenmu;
d2=lcmm/b.fenmu;
c.fenzi=d1*a.fenzi+d2*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
}
Sub.java
package cn.edu.nefu.zhihu;
public class Sub extends Yunsuanzi{
Object sub(Youlishu a,Youlishu b){ //注意:方法的重载,不是重写
Youlishu c=new Youlishu(0,0);
int d1,d2;
int lcmm=lcm(a.fenmu,b.fenmu);
c.fenmu=lcmm;
d1=lcmm/a.fenmu;
d2=lcmm/b.fenmu;
c.fenzi=d1*a.fenzi-d2*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
}
Multi.java
package cn.edu.nefu.zhihu;
public class Multi extends Yunsuanzi {
Object multi(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
c.fenzi=a.fenzi*b.fenzi;
c.fenmu=a.fenmu*b.fenmu;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
}
Divid.java
package cn.edu.nefu.zhihu;
public class Divid extends Yunsuanzi{
Object divid(Youlishu a,Youlishu b){
Youlishu c=new Youlishu(0,0);
c.fenzi=a.fenzi*b.fenmu;
c.fenmu=a.fenmu*b.fenzi;
int d=gcd(c.fenzi,c.fenmu);
c.fenzi=c.fenzi/d;
c.fenmu=c.fenmu/d;
return c;
}
}
Youlishu.java
package cn.edu.nefu.zhihu;
public class Youlishu extends Object{
int fenzi,fenmu;
Youlishu(){
}
Youlishu(int fenzi,int fenmu){
this.fenzi=fenzi;
this.fenmu=fenmu;
}
void show(){
System.out.println(fenzi+"/"+fenmu);
}
}
Test.java
package cn.edu.nefu.zhihu;
import java.util.Scanner;
public class test {
public static void main(String[] args){
Scanner reader=new Scanner(System.in);
int x1,y1,x2,y2;
x1=reader.nextInt();
y1=reader.nextInt();
x2=reader.nextInt();
y2=reader.nextInt();
Youlishu ylsa=new Youlishu(x1,y1);
Youlishu ylsb=new Youlishu(x2,y2);
Youlishu ylsc=new Youlishu(0,0);
Add jia=new Add();
ylsc=(Youlishu) jia.add(ylsa, ylsb);
ylsc.show();
Sub jian=new Sub();
ylsc=(Youlishu) jian.sub(ylsa, ylsb);
ylsc.show();
Multi cheng=new Multi();
ylsc=(Youlishu) cheng.multi(ylsa, ylsb);
ylsc.show();
Divid chu=new Divid();
ylsc=(Youlishu) chu.divid(ylsa, ylsb);
ylsc.show();
}
}
方法三代码实现:
Yunsuan.java
package cn.edu.nefu.zhihu;
public abstract class Yunsuan {
public abstract Object add(Object a);
public abstract Object sub(Object a);
public abstract Object multi(Object a);
public abstract Object divid(Object a);
public int gcd(int a,int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
public int lcm(int a,int b){
return a*b/gcd(a,b);
}
}
YouLiShu.java
package cn.edu.nefu.zhihu;
public class YouLiShu extends Yunsuan{
int fenzi,fenmu;
public YouLiShu(){}
public YouLiShu(int fenzi ,int fenmu){
this.fenzi=fenzi;
this.fenmu=fenmu;
}
@Override
public YouLiShu add(Object a) {
YouLiShu y=(YouLiShu)a;
YouLiShu z=new YouLiShu();
int lcmm=lcm(fenmu,y.fenmu);
z.fenmu=lcmm;
int d1,d2;
d1=lcmm/fenmu;
d2=lcmm/y.fenmu;
z.fenzi=d1*fenzi+d2*y.fenzi;
int gcdd2=gcd(z.fenmu,z.fenzi);
z.fenmu=z.fenmu/gcdd2;
z.fenzi=z.fenzi/gcdd2;
return z;
}
@Override
public YouLiShu divid(Object a) {
YouLiShu y=(YouLiShu)a;
YouLiShu z=new YouLiShu();
z.fenmu=fenmu*y.fenzi;
z.fenzi=fenzi*y.fenmu;
int gcdd2=gcd(z.fenmu,z.fenzi);
z.fenmu=z.fenmu/gcdd2;
z.fenzi=z.fenzi/gcdd2;
return z;
}
@Override
public YouLiShu multi(Object a) {
YouLiShu y=(YouLiShu)a;
YouLiShu z=new YouLiShu();
z.fenmu=fenmu*y.fenmu;
z.fenzi=fenzi*y.fenzi;
int gcdd2=gcd(z.fenmu,z.fenzi);
z.fenmu=z.fenmu/gcdd2;
z.fenzi=z.fenzi/gcdd2;
return z;
}
@Override
public YouLiShu sub(Object a) {
YouLiShu y=(YouLiShu)a;
YouLiShu z=new YouLiShu();
z.fenmu=fenmu*y.fenmu;
z.fenzi=fenzi*y.fenmu-fenmu*y.fenzi;
int gcdd2=gcd(z.fenmu,z.fenzi);
z.fenmu=z.fenmu/gcdd2;
z.fenzi=z.fenzi/gcdd2;
return z;
}
public String toString(){
return (fenzi+"/"+fenmu);
}
public double toXiaoShu(){
return fenzi*1.0/fenmu;
}
}
Test.java
package cn.edu.nefu.zhihu;
public class Test {
public static void main(String[] args) {
YouLiShu x=new YouLiShu(2,3);//匿名对象
YouLiShu y=new YouLiShu();
y=x.add(new YouLiShu(3,2));
System.out.println(y.toString());
System.out.println(y.toXiaoShu());
y=x.sub(new YouLiShu(1,3));
System.out.println(y.toString());
System.out.println(y.toXiaoShu());
y=x.multi(new YouLiShu(1,4));
System.out.println(y.toString());
System.out.println(y.toXiaoShu());
y=x.divid(new YouLiShu(3,2));
System.out.println(y.toString());
System.out.println(y.toXiaoShu());
}
}