test

1.

package lei1;

public class lei1 {

public int f(int a, int b) {

while(b != 0) {

int r = a % b;

a = b;

b = r;

}

return a;

}

}

class lei11 extends lei1{

@Override

public int f(int a, int b) {

int c = a * b;

while(b != 0) {

int r = a % b;

a = b;

b = r;

}

return c/a;

}

}

package lei1;

import java.util.Scanner;

public class test1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("第一个数:");

int a = scanner.nextInt();

System.out.print("第二个数:");

int b = scanner.nextInt();

lei1 yuelei = new lei1();

int yue = yuelei.f(a, b);

lei11 beilei = new lei11();

int bei = beilei.f(a, b);

System.out.println("最大公约数:" + yue);

System.out.println("最小公倍数:" + bei);

scanner.close();

}

}

2.

public abstract class AbstractClass {

public abstract void f(int x);

public abstract void g(int x, int y);

public abstract double h(double x);

}

public class SubClass1 extends AbstractClass {

@Override

public void f(int x) {

System.out.println("SubClass1 f: " + x);

}

@Override

public void g(int x, int y) {

System.out.println("SubClass1 g: " + x + ", " + y);

}

@Override

public double h(double x) {

return x * x;

}

}

public class SubClass2 extends AbstractClass {

@Override

public void f(int x) {

System.out.println("SubClass2 f: " + x);

}

@Override

public void g(int x, int y) {

System.out.println("SubClass2 g: " + x + ", " + y);

}

@Override

public double h(double x) {

return Math.sqrt(x);

}

}

public class SubClass3 extends AbstractClass {

@Override

public void f(int x) {

System.out.println("SubClass3 f: " + x);

}

@Override

public void g(int x, int y) {

System.out.println("SubClass3 g: " + x + ", " + y);

}

@Override

public double h(double x) {

return 1.0 / x;

}

}

public class MainClass {

public static void main(String[] args) {

AbstractClass obj1 = new SubClass1();

obj1.f(5);

obj1.g(10, 20);

double res1 = obj1.h(2.5);

System.out.println("Result 1: " + res1);

AbstractClass obj2 = new SubClass2();

obj2.f(8);

obj2.g(15, 25);

double res2 = obj2.h(5.0);

System.out.println("Result 2: " + res2);

AbstractClass obj3 = new SubClass3();

obj3.f(3);

obj3.g(12, 15);

double res3 = obj3.h(0.5);

System.out.println("Result 3: " + res3);

}

}

3.

public interface MyInterface {

void f(int x);

void g(int x, int y);

double h(double x);

}

public class MyClass implements MyInterface {

@Override

public void f(int x) {

System.out.println("MyClass f: " + x);

}

@Override

public void g(int x, int y) {

System.out.println("MyClass g: " + x + ", " + y);

}

@Override

public double h(double x) {

return Math.pow(x, 2);

}

}

public class MainClass {

public static void main(String[] args) {

MyClass myObj = new MyClass();

MyInterface callback = new MyInterface() {

@Override

public void f(int x) {

System.out.println("Callback f: " + x);

}

@Override

public void g(int x, int y) {

System.out.println("Callback g: " + x + ", " + y);

}

@Override

public double h(double x) {

return Math.sqrt(x);

}

};

myObj.f(5); // 调用MyClass的方法f()

myObj.g(10, 20); // 调用MyClass的方法g()

double res = myObj.h(2.5); // 调用MyClass的方法h()

System.out.println("Result: " + res);

callback.f(8); // 通过接口回调调用Callback的方法f()

callback.g(15, 25); // 通过接口回调调用Callback的方法g()

double res2 = callback.h(5.0); // 通过接口回调调用Callback的方法h()

System.out.println("Result from callback: " + res2);

}

}