Java--抽象类与接口

1.package www.sdlzy.java;
interface InterfaceA{
String S=“good”;
void f();
}
abstract class ClassA {
abstract void g();
}
class ClassB extends ClassA implements InterfaceA {
void g() {
System.out.print(S);
}
public void f() {
System.out.print(" "+ S);
}
}
public class Test2 {
public static void main(String[] args) {
//子类Class B向上转型对抽象父类Class A进行实例化
ClassA a = new ClassB();
//子类Class B向上转型对父类接口Interface A进行实例化
InterfaceA b = new ClassB();
//结果:good good
a.g();//good
b.f();// good
}
}
2.2.编程题:
利用接口做参数,写个计算器,能完成加减乘除运算。
(1)定义一个接口Compute含有一个方法int computer(int n, int m)。
(2)设计四个类分别实现此接口,完成加减乘除运算。
(3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。
package www.sdlzy.java;
//(1)定义一个接口Compute含有一个方法int computer(int n, int m)。
interface ICompute{
int computer(int n,int m);
}

//(2)设计四个类分别实现此接口,完成加减乘除运算。
class Add implements ICompute{
public int computer(int n,int m){
return n+m;
}

}
class Sub implements ICompute{
public int computer(int n,int m){
return n-m;
}
}
class Mul implements ICompute{
public int computer(int n,int m){
return n*m;
}
}
class Div implements ICompute{
public int computer(int n,int m){
if(m!=0) {
return n / m;
}
System.out.println(“输入数值错误!”);
return 0;
}
}

// (3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),
// 此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。
class UseCompute{
public void useCom(ICompute com,int m,int n){
System.out.println(com.computer(m,n));
}
}
public class Test4 {
public static void main(String[] args){
UseCompute uc=new UseCompute();
Add a1=new Add();
Sub a2=new Sub();
Mul a3=new Mul();
Div a4=new Div();
System.out.println(“加法:”);uc.useCom(a1,2,3);
System.out.println(“减法:”);uc.useCom(a2,2,3);
System.out.println(“乘法:”);uc.useCom(a3,2,3);
System.out.println(“除法:”);uc.useCom(a4,2,3);

}

}
在这里插入图片描述

3.按如下要求编写Java程序:
(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
(2)定义接口B,里面包含抽象方法void setColor(String c)。
(3)定义接口C,该接口继承了接口A
和B,里面包含抽象方法void volume()。
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
(5)创建主类来测试类Cylinder。
package www.sdlzy.java;
//(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
interface A{
double PI=3.14;
double area(int r,int h);
}

// (2)定义接口B,里面包含抽象方法void setColor(String c)。
interface B{
void setColor(String c);

}

// (3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
interface C extends A,B{
void volume();

}

//(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、圆柱体的高height、颜色color。
class Cylinder implements C{
double r;
double h;
String color;

public double area(int r,int h){
    System.out.println("面积是:"+2*PI*r*r+2*PI*r*h);
    return 2*PI*r*r+2*PI*r*h;
}

public void setColor(String color) {
    this.color = color;
    System.out.println("颜色:"+this.color);
}

public void volume() {

    System.out.println("圆柱的体积:"+area(1,5)*h);

}

}

// (5)创建主类来测试类Cylinder。
public class Test8 {
public static void main(String[] args){
A a = new Cylinder() ;
a.area(5,9);
Cylinder cylinder = (Cylinder) a;
cylinder.setColor(“red”);
cylinder.volume();

}

}

4.(附加题-算法)
一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。
package www.sdlzy.java;
public class Test3 {
public static void main(String[] args) {
for (int i = 1; i < 1000; i++) {
if (isWanShu(i)) {
System.out.print(i + " ");
}
}
}

    public static boolean isWanShu ( int num) {
        int total = 0;
        for (int j = 1; j < num; j++) {
            //此时j为因子
            if (num % j == 0) {
                total += j;
            }
        }
        if (total == num) {
            return true;
        }

        return false;

    }

}
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值