`

java作業3

    博客分类:
  • java
阅读更多
package homework03;
/*
* 首先编写一个抽象类,要求该类有如下3个抽象方法:
* public abstract void f(int x);
* public abstract void g(int x,int y);
* public abstract double h(double x);
* 然后分别给出该类的3个子类。要求:在应用程序的主类中使用这些子类创建对象,然后让它们的
* 上转型对象调用方法:f(),g()和h()。
*/
abstract class F{
public abstract void f(int x);
public abstract void g(int x,int y);
public abstract double h(double x);
}
class s1 extends F{
public void f(int x) {
System.out.println("子类1的f函数 打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类1的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类1的h函数 打印参数(double):"+x);
return x;
}
}
class s2 extends F{
public void f(int x) {
System.out.println("子类2的f函数打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类2的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类2 h函数 打印参数(double):"+x);
return x;
}
}
class s3 extends F{
public void f(int x) {
System.out.println("子类3的f函数 打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类3的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类3的h函数 打印参数(double):"+x);
return x;
}
}
public class ch05_2 {
public static void main(String[] args) {
F testF=new s1(); //上转对象
testF.f(2);
testF.g(2, 2);
testF.h(2);

testF=new s2(); //上转对象
testF.f(2);
testF.g(2, 2);
testF.h(2);

testF=new s3();//上转对象
testF.f(2);
testF.g(2, 2);
testF.h(2);

}
}
package homework03;
/*
* 编写一个类,要求该类实现一个接口,该接口有如下3个抽象方法:
* public abstract void f(int x);
* public abstract void g(int x,int y);
* public abstract double h(double x);
* 要求:在应用程序的主类中使用该类创建对象,并使用接口回调来调用这些方法:f(),g()和h();
*/
interface InterfaceTest{
public abstract void f(int x);
public abstract void g(int x,int y);
public abstract double h(double x);
}
class a1 implements InterfaceTest{
public void f(int x) {
System.out.println("子类1的f函数 打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类1的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类1的h函数 打印参数(double):"+x);
return x;
}
}
class a2 implements InterfaceTest{
public void f(int x) {
System.out.println("子类2的f函数打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类2的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类2 h函数 打印参数(double):"+x);
return x;
}
}
class a3 implements InterfaceTest{
public void f(int x) {
System.out.println("子类3的f函数 打印参数:"+x);
}
public void g(int x, int y) {
System.out.println("子类3的g函数 打印参数(int)x=:"+x+" y="+y);
}
public double h(double x) {
System.out.println("子类3的h函数 打印参数(double):"+x);
return x;
}
}
public class ch05_3 {
public static void main(String[] args) {
InterfaceTest testF=new a1(); //接口回调
testF.f(2);
testF.g(2, 2);
testF.h(2);

testF=new a2(); //接口回调
testF.f(2);
testF.g(2, 2);
testF.h(2);

testF=new a3();//接口回调
testF.f(2);
testF.g(2, 2);
testF.h(2);

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics