代码如下:
// BTest.javapackage com.baidu.demo036
abstract class A {
public abstract void f()
public void g() {
System.out.println("Hello")
}
}
class B extends A {
@Override
public void f() {
System.out.println("Hi")
}
}
public class BTest {
public static void main(String[] args) {
B b = new B()
b.f()
b.g()
}
} // RectTest.java
package com.baidu.demo036
interface Shape {
double computeArea()
}
class Rect implements Shape {
private double width
private double height
public Rect(double width, double height) {
this.width = width
this.height = height
}
@Override
public double computeArea() {
return width * height
}
}
public class RectTest {
public static void main(String[] args) {
Rect rect = new Rect(10, 30)
System.out.println("Are: " + rect.computeArea())
}
}
用 Eclipse 创建一个java项目,把这两个文件放进去运行就可以了。
class Ball {public void play() {
System.out.println("玩球儿...")
}
}
class Football extends Ball {
public void play() {
System.out.println("使用足球运动")
}
}
class Basketball extends Ball {
public void play() {
System.out.println("使用篮球运动")
}
}
public class TestMain {
public static void main(String[] args) {
TestMain tm = new TestMain()
tm.testPlay()
}
public void testPlay() {
Ball ball = new Football()
ball.play()
ball = new Basketball()
ball.play()
}
}
/*
D:\>javac TestMain.java
D:\>java TestMain
使用足球运动
使用篮球运动
*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)