实践五.2 子类与继承
- 对象的上转型对象。
class A {
int a = 1, c = 9;
void showa() {
System.out.println("In A:a=" + a);
}
void showc() {
System.out.println("In A:c=" + c);
}
}
class B extends A {
int a = 2, b = 4;
void showa() {
System.out.println("In B:a=" + a);
}
void showb() {
System.out.println("In B:b=" + b);
}
}
public class test {
public static void main(String args[]) {
A b=new B();
b.showa();
b.showc();
System.out.println(b.a);
System.out.println(b.c);
//b.showb();
//System.out.println(b.b);
}
}
总结:通过上转型对象b可以访问的是(A B D )。
A.父类成员变量 B.父类中的未被子类重写的方法 C.子类中成员变量
D.子类中重写的方法 E.子类中新增的方法 F.父类中的被子类重写的方法
运行结果:
In B:a=2
In A:c=9
1
9
2.对象的上转型对象应用。
import java.util.Scanner;
class Animal{
String name;
int age;
//定义无参构造方法,输出I am ananimal.
public Animal(){
System.out.println("I am ananimal.");
}
Animal(String name,int age)
{//定义构造方法,给成员变量赋值,并输出My name is:XXX. I'm X-year old.
this.name=name;this.age=age;
System.out.println("My name is:"+name+". I'm "+age+"-year old.");
}
void ability(){
System.out.println("I can eat and move.");
}
}
class Bird extends Animal{
public Bird(String name, int age) {
super(name, age);
}
void ability(){
super.ability();
System.out.println("I can fly.");
}
}
//定义Animal的子类Bird,调用父类构造方法给成员变量赋值,覆盖ability方法,
//在其中调用父类该方法,并输出:I can fly.
//定义Animal的子类Fish,调用父类构造方法给成员变量赋值,覆盖ability方法,
//在其中调用父类该方法,并输出:I can swim.
class Fish extends Animal{
public Fish(String name, int age) {
super(name, age);
}
void ability(){
super.ability();
System.out.println("I can swim.");
}
}
public class test {
public static void main(String[] args){
Scanner r=new Scanner(System.in);
System.out.println("1-Bird 2-Fish other number-other animal:");
System.out.println("Please input an animal you like best:");
int t=r.nextInt();
if(t==1) show(new Bird("Bird Koko",2));
else if(t==2) show(new Fish("Fish Jony",3));
else show(new Animal());
}
static void show(Animal like){
like.ability();
}
}
运行结果:
1-Bird 2-Fish other number-other animal:
Please input an animal you like best:
1
My name is:Bird Koko. I'm 2-year old.
I can eat and move.
I can fly.
3.抽象类与抽象方法
abstract class A {
abstract int sum(int x,int y) ;//声明抽象求和方法
int sub(int x,int y){
return x-y;
}//求差方法
}
class B extends A { //子类必须重写父类的抽象方法
int sum(int x,int y) {
return x+y;
}//调用求和方法
}
public class text {
public static void main(String args[]) {
A b=new B();
int sum=b.sum(5,9); //调用求和方法
int sub=b.sub(7,4); //调用相减方法
System.out.println("sum="+sum);
System.out.println("sum="+sub);
}}
运行结果:
sum=14
sum=3
4. 定义形状抽象类 Shape 并派生出Circle和Rectangle类。
abstract class Shape
{ protected String name;
public Shape(String xm)
{ name=xm; System.out.print("名称:"+name); }
abstract public double getArea();
abstract public double getLength();
}
class Circle extends Shape
{ private final double PI=3.14;
private double radius;
public Circle(String Name,double r)
{ super(Name); radius=r; }
public double getArea()
{ return PI*radius*radius; }
public double getLength()
{ return 2*PI*radius; }
}
class Rectangle extends Shape
{ private final double PI=3.14;
private double x,y;
public Rectangle(String xm, double x, double y) {
super(xm);
this.x = x;
this.y = y;
}
public double getArea()
{ return x*y; }
public double getLength()
{ return 2*(x+y); }
}; //定义长方形Rectangle类继承Shape类
public class test
{ public static void main(String[] args)
{ Shape shape =new Rectangle( "长方形",6.5,10.3 );
System.out.print("; 面积="+shape.getArea() );
System.out.println("; 周长="+shape.getLength() );
shape=new Circle("圆",10.2);
System.out.print( "; 面积="+shape.getArea() );
System.out.println( "; 周长="+shape.getLength() );
}}
运行结果:
名称:长方形; 面积=66.95; 周长=33.6
名称:圆; 面积=326.68559999999997; 周长=64.056