类的继承和多态

1.设计一个抽象类,声明有关算术运算的方法,并创建四个子类继承该抽象的类,完成进行加、减、乘、除四则运算。

publicabstract  class  calculate {
      public abstract double A(double a,doubleb);
}
public class addextends calculate{
            public double A(double a,double b){
               return a+b;
            }
}
public class subextends calculate{
      public double A(double a,double b){
          return a-b;
       }
}
public class mulextends calculate {
      public double A(double a,double b){
          return a*b;
       }
}
public class divextends calculate{
      public double A(double a,double b){
           return a/b;
       }
}
public class Main {
      public static void main(String[] args) {
           add h1=new add();
           sub h2=new sub();
           mul h3=new mul();
           div h4=new div();
           System.out.println("1+2="+h1.A(1,2)+" 1-2="+h2.A(1, 2)+" 1*2="+h3.A(1, 2)+"1/2="+h4.A(1, 2));
      }
}


2.编写一个类,该类有如下一个方法:

public int f(inta,int b){……//要求该方法将返回a和b的最大公约数},再编写一个该类的子类,要求子类重写方法f(),而且重写的方法将返回两个正整数的最小公倍数。

public class lcmextends gcd{
      public int f(int a,int b){
           gcd tem=new gcd();
           return a*b/tem.f(a, b);
      }
}
public class gcd {
      public int f(int a,int b){
           if(a<b){
                 int t=a;a=b;b=t;
           }
           while(a%b!=0){
                 int t=a%b;
                 a=b;b=t;
           }
           return b;
      }
}
public class Main {
      public static void main(String[] args) {
           gcd A=new gcd();
           lcm B=new lcm();
           System.out.println("14和21的最大公约数="+A.f(14, 21)+"\n14和21的最小公倍数="+B.f(14,21));
      }
}

3.编写一个圆锥类,能够计算圆锥的体积,要求圆锥类的构造函数中有一参数是圆类的一个对象。

public class yuanzhui {
	double v;
	yuanzhui(double r,double h)
	{
		yuan S=new yuan(r);
		v=S.s*h/3.0;
	}
	yuanzhui(){}
	void set(double r,double h)
	{
		yuan S=new yuan(r);
		v=S.s*h/3.0;
	}
}

public class yuan {
	double s;
	yuan(double r){
		s=3.14*r*r;
	}
	yuan(){}
	void set(double r)
	{
		s=3.14*r*r;
	}
}
public class Main {
	public static void main(String[] args) {
	yuanzhui a1=new yuanzhui(5,3);
	yuanzhui a2=new yuanzhui();
	a2.set(5,3);
	System.out.println("a1的体积="+a1.v+"  a2的体积="+a2.v);
	}
}



4.定义接口Shape,其中包括Area方法。类Circle、Square和Triangle均实现了接口Shape。定义主函数,创建元素个数为3的Shape类型的一维数组,分别为数组元素创建Circle、Square和Triangle类型的对象,最后分别调用各数组元素的Area方法,输出相关信息。

public interface Shape{
	double Area();
}
public class Circle implements Shape{
	double s;
	Circle(double r){
		s=r*r*3.14;
	}
	public double Area() {
		return s;
	}
}
public class Square implements Shape{
	double s;
	Square(double l){
		s=l*l;
	}
	public double Area() {
		return s;
	}
}
public class Triangle implements Shape{
	double s;
	Triangle(double l,double w){
		s=l*w/2.0;
	}
	public double Area() {
		return s;
	}
}
public class gbb {
	public static void main(String[] args) {
		Shape []a={ new Circle(2),new Square(4),new Triangle(3,3)};
		System.out.println("a[0]是圆,半径2,面积为:"+a[0].Area()+"   a[1]是正方形,边长4,面积为:"+a[1].Area()+"    a[2]是三角形,底长3,高为3,面积为:"+a[2].Area());
	}
}





转载于:https://www.cnblogs.com/martinue/p/5490420.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值