(1)
package Test;
interface View{
void display();
}
class Notice implements View{
public void display(){
System.out.println("通知内容");
}
}
class Car implements View{
public void display() {
System.out.println("汽车油量");
}
}
class Ad implements View{
public void display() {
System.out.println("广告消息");
}
}
public class test
{
public static void main(String[] args){
View x = new Notice();
x.display();
x = new Car();
x.display();
x = new Ad();
x.display();
}
}
(2)
package Test;
interface Shape{
double size();
}
class Rec implements Shape{
int l,h;//长宽
public Rec() {
super();
this.l=0;
this.h=0;
}
public Rec(int l, int h) {
super();
this.l = l;
this.h = h;
}
public double size() {
return l*h;
}
}
class Circular implements Shape{
int r;//半径
public Circular() {
super();
this.r=0;
}
public Circular(int r) {
super();
this.r = r;
}
public double size() {
return Math.PI*r*r;
}
}
class Cylinder implements Shape{
int r,h;//底面半径,高
public Cylinder() {
super();
this.r = 0;
this.h = 0;
}
public Cylinder(int r, int h) {
super();
this.r = r;
this.h = h;
}
public double size() {
return Math.PI*r*r*h;
}
}
public class test
{
public static void main(String[] args){
Shape[] x=new Shape[3];
x[0] = new Rec(1,2);
x[1] = new Circular(2);
x[2] = new Cylinder(3,4);
for(Shape i:x) {
System.out.println(i.size());
}
}
}
(3)
package Test;
abstract class Fruit{
public Fruit() {
super();
}
abstract int getWeight();
}
class Apple extends Fruit{
private int w;//重量
public Apple(int w) {
super();
this.w = w;
}
int getWeight() {
return w;
}
}
class Peach extends Fruit{
private int w;//重量
public Peach(int w) {
super();
this.w = w;
}
int getWeight() {
return w;
}
}
class Orange extends Fruit{
private int w;//重量
public Orange(int w) {
super();
this.w = w;
}
int getWeight() {
return w;
}
}
public class test
{
public static void main(String[] args){
Fruit[] x = new Fruit[3];
x[0] = new Apple(1);
x[1] = new Peach(2);
x[2] = new Orange(3);
for(Fruit i:x) {
System.out.println(i.getClass().getName()+"重 :"+i.getWeight()+"kg");
}
}
}