5.1 继承
class A
{ int i,j;
void showIJ()
{ System.out.println("I and J: "+i+" "+j);}
}
class B extends A
{void showK(int k)
{System.out.println("I+J+K="+(i+j+k)); }
}
class SimpleInher
{public static void main(String args[])
{ A superOb=new A();
B subOb=new B();
superOb.i=12; superOb.j=121;
superOb.showIJ();
subOb.showIJ();
subOb.showK(3);
}
}
二、子类的继承性
- 若子类与父类在同一个包中,则子类可以继承父类非私有的成员变量或成员方法,并保持访问权限不变。
- 若子类与父类不在同一个包中,则子类可以继承父类共有的和受保护的成员变量或成员方法,并保持访问权限不变。
访问权限
不能,因为main是静态方法,只能调用同一个类中的其它静态方法,对同一个类中的非静态方法的调用必须使用对象名。
输出结果是:1000
1000
若f()方法的money=1000;改为 int money=1000;则输出结果为1000 / 120
package tom.jiafei;
public class Father
{
int height;
protected int money=120;
protected int getMoney()
{
return money;
}
void setMoney(int newMoney)
{
money=newMoney;
}
}
package sun.com;
import tom.jiafei.Father;
public class Jerry extends Father
{
void f()
{ money=1000;
height=1.89f;
System.out.println(money);
setMoney(300);
int number=getMoney();
System.out.println(number);
}
public static void main(String args[])
{
Jerry jerry=new Jerry();
jerry.f();
}
}
class SuperClass
{ public int a(int x)
{ return x*x;
}
}
class SubClass extends SuperClass
{ public int a(int y)
{ return super.a(y)+y;
}
}
class Recover
{ public static void main(String args[])
{int m=-12;
SubClass sub_a=new SubClass();
System.out.println(sub_a.a(m));
}
}
class CallingCons
{ public static void main(String args[])
{
C xxxx=new C();
}
}
class A
{A()
{ System.out.println("Inside A's constructor.");
}
}
class B extends A
{B()
{ System.out.println("Inside B's constructor.");
}
}
class C extends B
{C()
{ System.out.println("Inside C's constructor.");
}
}
class CallingCons
{ public static void main(String args[])
{
C xxxx=new C();
}
}
class A
{ A(int x)
{ System.out.println("x="+x);
}
}
class B extends A
{B()
{ super(3);
System.out.println("Inside B's constructor.");
}
}
class C extends B
{C()
{ System.out.println("Inside C's constructor.");
}
}
三、super关键字
访问权限
访问权限与继承
5.2 对象的上转型对象
二、上转型对象的特点
很重要! 如果子类重写了父类的静态方法,那么子类对象的上转型对象不能调用子类重写的静态方法,只能调用父类的静态方法。
例1:
abstract class Figure
{ double dim1,dim2;
Figure(double a,double b)
{ dim1=a;
dim2=b;
}
abstract double area();
}
class Rectangle extends Figure
{ Rectangle(double a,double b)
{ super(a,b);
}
double area()
{ System.out.println("Inside Area for Rectangle.");
return dim1*dim2;
}
}
class Triangle extends Figure
{ Triangle(double a,double b)
{ super(a,b);
}
double area()
{ System.out.println("Inside Area for Triangle.");
return dim1*dim2/2;
}
}
public class AbstractAreas
{ public static void main(String args[])
{ Rectangle r=new Rectangle(9,5);
Triangle t=new Triangle(10,8);
Figure figref;
figref=r;
System.out.println("Area is "+figref.area());
figref=t;
System.out.println("Area is "+figref.area());
}
}
例2:
class Box
{ double width,height,depth;
Box()
{ width=1; height=1; depth=1; }
double volume()
{ return width*height*depth; }
}
class BoxWeight extends Box
{ double weight;
BoxWeight(double w,double h,double d,double m)
{ width=w; height=h; depth=d; weight=m;}
double getWeight()
{ return weight*10; }
}
class RefDome
{ public static void main(String args[])
{ Box plainbox=new Box();
BoxWeight weightbox=new BoxWeight(3,5,7,8.37);
System.out.println("Volume of weightbox is "+weightbox.volume());
System.out.println("Weight of weightbox is "+weightbox.weight);
System.out.println("Weight of weightbox is "+weightbox.getWeight());
System.out.println(" Volume of plainbox is "+ plainbox.volume() );
plainbox=weightbox;
System.out.println("Volume of plainbox is "+plainbox.volume() );
System.out.println("Weight of plainbox is "+plainbox.weight); //错
System.out.println("Weight of plainbox is "+plainbox.getWeight()); //错
}
}
注:
- 不要将父类创建的对象和子类创建的对象的上转型对象混淆;
- 可以将对象的上转型对象再强制转换到一个子类对象,这时,该子类对象又具备了子类所有的属性和功能;
- 不可以将父类创建的对象的引用赋值给子类声明的对象。
A r;改成:B r;或C r; 不可以!!!
class A
{ void callme()
{System.out.println("Inside A's callme methods"); }
}
class B extends A
{ void callme()
{System.out.println("Inside B's callme methods"); }
}
class C extends A
{ void callme()
{System.out.println("Inside C's callme methods"); }
}
class Dispatch
{ public static void main(String args[])
{ A r;
r=new A();
r.callme();
r=new B();
r.callme();
r=new C();
r.callme();
}
}
5.3 多态与重载
构造方法重载
class Sub_Over
{ int i;
Sub_Over()
{System.out.println("this is the first constructor");
}
Sub_Over(int n)
{ this();
i=n;
System.out.println("this is another constructor");
System.out.println("the parameter n value is: "+i);
}
}
public class OverLoad
{ public static void main(String args[])
{ Sub_Over mm=new Sub_Over(12);
}
}
输出结果:
this is the first constructor
this is another constructor
the parameter n value is: 12
二、方法重载
- 同一个类(或子类与父类)中的两个或两个以上的方法共用一个名字,但参数不同,这样的方法被称为重载。(overloaded);
- 构造方法也可以重载,这些构造方法可以互相调用,一个方法调用另一个构造方法时,使用关键字this,同时,这个调用语句应该是该构造方法的第一个可执行语句。
【练习1】
class A
{ double f(double x, float y)
{return x+y;}
double f(float x,float y)
{return x*y;}
}
public class E
{ public static void main(String args[])
{ A a=new A();
System.out.println(“**”+a.f(10,10));
System.out.println(“##”+a.f(10.0d,10.0f));
}
}
说出运行结果:
**100
##10
【练习2】
class A
{ double f(double x, double y)
{return x+y;}
}
class B extends A
{ double f(int x,int y)
{return x*y;}
}
public class E
{ public static void main(String args[ ])
{ B b=new B();
System.out.println(b.f(3,5));
System.out.println(b.f(3.0,5.0));
}
}
运行结果:
15.0
8.0
【练习3】
class A
{ double f(double x, double y)
{return x+y;}
static int g(int n)
{return n*n;}
}
class B extends A
{ double f(double x,double y)
{ double m=super.f(x,y);
return m+x*y;}
static int g(int n)
{ int m=A.g(n);
return m+n;
}
}
public class E
{ public static void main(String args[])
{ B b=new B();
System.out.println(b.f(10.0,8.0));
System.out.println(b.g(b.g(3)));
}
}
运行结果:
98.0
12
上机实践4-实验1 继承
Example.java
class People
{
protected double weight,height;
public void speakHello()
{
System.out.println("yayawawa");
}
public void averageHeight()
{
height=173;
System.out.println("average height:"+height);
}
public void averageWeight()
{
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People
{
【代码1】
//重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的汉语信息
【代码2】 //重写public void averageHeight()方法,要求输出类似中国人的平均身高:168.78厘米”这样的汉语信息
【代码3】 //重写public void averageWeight()方法,
//要求输出类似“中国人的平均体重:65公斤”这样的汉语信息
public void chinaGongfu()
{
【代码4】//输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等
}
}
class AmericanPeople extends People
{
【代码5】 //重写public void speakHello()方法,要求输出类似 “How do you do”这样的英语信息。
【代码6】 //重写public void averageHeight()方法
【代码7】 //重写public void averageWeight()方法
public void americanBoxing()
{
【代码8】//输出拳击的信息,例如,“直拳”、“钩拳”等
}
class BeijingPeople extends ChinaPeople
{
【代码9】 //重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息
【代码10】 //重写public void averageHeight()方法
【代码11】 //重写public void averageWeight()方法
public void beijingOpera()
{
【代码12】//输出京剧的信息
}
}
public class Example
{
public static void main(String args[])
{
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}
上机实践4-实验2 上转型对象
HardWork.java
abstract class Employee
{
public abstract double earnings();
}
class YearWorker extends Employee
{
【代码1】 //重写earnings()方法
}
class MonthWorker extends Employee
{
【代码2】 //重写earnings()方法。
}
class WeekWorker extends Employee
{
【代码3】 //重写earnings()方法。
}
class Company
{
Employee[] employee;
double salaries=0;
Company(Employee[] employee)
{
this.employee=employee;
}
public double salariesPay()
{
salaries=0;
【代码4】 //计算salaries。
return salaries;
}
}
public class HardWork
{
public static void main(String args[])
{
Employee[] employee=new Employee[20];
for(int i=0;i<employee.length;i++)
{
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
}
Company company=new Company(employee);
System.out.println("公司年工资总额:"+company.salariesPay());
}
}