public class C {
public static void main(String[] args) {
A a = new A();
a.f();
B b = new B();
b.f();
b.g();
}
}
class A {
void f() {
System.out.println("abcdefghijklmnopqrstuvwxyz");
}
}
class B extends A {
void g() {
System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
}
按要求编写一个Java应用程序:
(1)编写一个矩形类Rect,包含:两个protected属性:矩形的宽width;矩形的高height。
两个构造方法:
1.一个带有两个参数的构造方法,用于将width和height属性初化;
2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。
两个方法:
求矩形面积的方法area()
求矩形周长的方法perimeter()
(2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用矩形的左上角坐标来标识,包含:添加两个属性:矩形左上角坐标startX和startY。
两个构造方法:
带4个参数的构造方法,用于对startX、startY、width和height属性
初始化;
不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;
添加一个方法:
判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true, 否则,返回false。
提示:点在矩形类是指满足条件:
x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)
(3)编写PlainRect类的测试程序
创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;计算并打印输出矩形的面积和周长;判断点(25.5,13)是否在矩形内,并打印输出相关信息。
public class Test2 {
public static void main(String[] args) {
PlainRect p = new PlainRect(10, 20, 10, 10);
System.out.println(p.area());
System.out.println(p.perimeter());
if (p.isInside(25.5, 13) == true) System.out.println("在内部");
else System.out.println("不在内部");
}
}
class Rect {
protected double width;
protected double height;
Rect(double w, double h) {
width = w;
height = h;
}
Rect() {
width = 10;
height = 10;
}
double area() {
return width * height;
}
double perimeter() {
return (width + height) * 2;
}
}
class PlainRect extends Rect {
double startX;
double startY;
PlainRect(double w, double h, double x, double y) {
width = w;
height = h;
startX = x;
startY = y;
}
PlainRect() {
width = 0;
height = 0;
startX = 0;
startY = 0;
}
boolean isInside(double x, double y) {
if (x >= startX && x <= (startX + width) && y < startY && y >= (startY - height)) return true;
else return false;
}
}
1.按要求编写Java程序:
(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);
(2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算1到n的和;
(3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算n的阶乘(n!);
(4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现接口的类。
public class E {
public static void main(String[] args) {
InterfaceA a;
a = new ClassA();
System.out.println(a.method(3));
a = new ClassB();
System.out.println(a.method(3));
}
}
interface InterfaceA {
int method(int n);
}
class ClassA implements InterfaceA {
public int method(int n) {
int result = 0;
for (int temp = 1; temp <= n; ++temp) {
result += temp;
}
return result;
}
}
class ClassB implements InterfaceA {
public int method(int n) {
if (n == 1 || n == 0) {
return 1;
}
int result = 1;
for (int temp = 1; temp <= n; ++temp) {
result *= temp;
}
return result;
}
}
public class Adventure {
public static void main(String[] args) {
Hero hb = new Hero("haha");
hb.swim();
hb.fight();
hb.fly();
Canfly cf = hb;
cf.fly();
CanSwim cs = hb;
cs.swim();
ActionCharacter ac = hb;
ac.fight();
ac.speak();
}
}
interface CanSwim {
void swim();
}
interface Canfly {
void fly();
}
abstract class ActionCharacter {
void fight() {}
void speak() {}
}
class Hero extends ActionCharacter implements Canfly, CanSwim {
String name;
Hero(String name) {
this.name = name;
}
public void fly() {
System.out.println(name + "飞");
}
public void swim() {
System.out.println(name + "游泳");
}
public void fight() {
System.out.println(name + "打架");
}
public void speak() {
System.out.println(name + "说话");
}
}
3.利用接口做参数,写个计算器,能完成+-*/运算
(1)定义一个接口Compute含有一个方法int computer(int n,int m);
(2)设计四个类分别实现此接口,完成+-*/运算
(3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)
此方法要求能够:1.用传递过来的对象调用computer方法完成运算 2.输出运算的结果
(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算
public class Test {
public static void main(String[] args) {
Computer c = new Plus();
UseCompute u = new UseCompute();
u.useCom(c, 2, 3);
c = new Minus();
u = new UseCompute();
u.useCom(c, 2, 3);
c = new Multiplication();
u = new UseCompute();
u.useCom(c, 2, 3);
c = new Division();
u = new UseCompute();
u.useCom(c, 2, 3);
}
}
interface Computer {
int computer(int n, int m);
}
class Plus implements Computer {
public int computer(int n, int m) {
return (n + m);
}
}
class Minus implements Computer {
public int computer(int n, int m) {
return (n - m);
}
}
class Multiplication implements Computer {
public int computer(int n, int m) {
return (n * m);
}
}
class Division implements Computer {
public int computer(int n, int m) {
return (n / m);
}
}
class UseCompute {
public void useCom(Computer com, int one, int two) {
System.out.println(com.computer(one, two));
}
}