201711671112 《JAVA程序设计》第五章学习总结
教材学习内容总结
1、继承是一种由已有的类创建新类的机制。JAVA的类按继承方式可以先定义一个共有属性的一般类,根据该一般类再定义具有特殊属性的子类,子类继承一般类的属性和行为,并根据需要增加它自己的新的属性和行为。
2、java不支持多重继承,子类只能有一个父类。通过使用关键字extends来定义一个子类。class 子类 extends 父类
3、java的类按继承关系形成树形结构,根结点是Object类,是所有类的祖先类。每个类(除了object类)有且仅有一个父类。如果一个类的声明中没有使用extends关键字,这个类被系统默认为是object的子类。
4、如果子类和父类在同一个包中
子类自然继承了其父类中不是private的成员变量和方法,继承的成员变量和方法访问权限保持不变
example5_1
package Example5_1;
class People{
int age,leg=2,hand=2;
protected void showPeopleMess() {
System.out.printf("%d岁,%d只脚,%d只手\t",age,leg,hand);
}
}
class Student extends People{
int number;
void tellNumber() {
System.out.printf("学号:%d\t",number);
}
int add(int x,int y) {
return x+y;
}
}
class UniverStudent extends Student{
int multi(int x,int y) {
return x*y;
}
}
public class Example5_1 {
public static void main(String[] args) {
Student zhang=new Student();
zhang.age=17;
zhang.number=100101;
zhang.showPeopleMess();
zhang.tellNumber();
int x=9,y=29;
System.out.print("会做加法:");
int result=zhang.add(x, y);
System.out.printf("%d+%d=%d\n",x,y,result);
UniverStudent geng=new UniverStudent();
geng.age=21;
geng.number=6609;
geng.showPeopleMess();
geng.tellNumber();
System.out.print("会做加法:");
result=geng.add(x, y);
System.out.printf("%d+%d=%d\t",x,y,result);
System.out.print("会做乘法:");
result=geng.multi(x, y);
System.out.printf("%dx%d=%d\n",x,y,result);
}
}
如果子类和父类不在同一个包中
子类只继承父类中的protected和public访问权限的成员变量和方法。
5、当用子类的的构造方法创建一个子类对象时,不仅子类声明的成员变量被分配了内存,而且父类的成员变量也分配了。子类的构造方法:先调用父类的某个构造方法,完成父类部分的创建,再调用子类的,创建子类部分。
example5_2
package Example5_2;
class People{
private int averHeight=166;
public int getAverHeight() {
return averHeight;
}
}
class ChinaPeople extends People{
int height;
public void setHeight(int h) {
height=h;
}
public int getHeight() {
return height;
}
}
public class Example5_2 {
public static void main(String[] args) {
ChinaPeople zhangsan=new ChinaPeople();
System.out.println("子类对象未继承的averageHeight的值是:"+zhangsan.getAverHeight());
zhangsan.setHeight(178);
System.out.println("子类对象的实例变量height值是:"+zhangsan.getHeight());
}
}
6、所声明的成员变量名字和从父类继承来的相同(声明类型可以不同),子类就会隐藏所继承的成员变量。
注:子类继承的方法只能操作子类继承和隐藏的成员变量。子类新定义的方法可以作为子类继承和子类声明的成员变量,但无法操作子类隐藏的成员变量。
example5_3
package Example5_3;
class Goods{
public double weight;
public void oldSetWeight(double w) {
weight=w;
System.out.println("double型的weight="+weight);
}
public double oldGetPrice() {
double price=weight*10;
return price;
}
}
class CheapGoods extends Goods{
public int weight;
public void newSetWeight(int w) {
weight=w;
System.out.println("int型的weight="+weight);
}
public double newGetPrice() {
double price=weight*10;
return price;
}
}
public class Example5_3 {
public static void main(String[] args) {
CheapGoods cheapGoods=new CheapGoods();
cheapGoods.newSetWeight(198);
System.out.println("对象cheapGoods的weight的值是:"+cheapGoods.weight);
System.out.println("cheapGoods用子类新增的优惠方法计算价格:"+cheapGoods.newGetPrice());
cheapGoods.oldSetWeight(198.987);//子类对象调用继承的方法操作隐藏的double型变量weight
System.out.println("cheapGood使用继承的方法计算价格:"+cheapGoods.oldGetPrice());
}
}
7、子类通过重写可以隐藏已继承的方法。
8、重写的语法规则
①子类中定义一个方法,这个方法的类型和父类的方法类型一致,或者是父类的方法的类型的子类型。
②这个方法的名字、参数个数、参数的类型和父类的方法完全相同。
子类如此定义的方法称作子类重写的方法。
③重写的目的
子类通过方法的重写可以隐藏继承的方法,子类通过方法的重写可以吧父类的状态和行为改变为自身的状态和行为。
重写方法既可以操作继承的成员变量、调用继承的方法,也可以操作子类新声明的成员变量、调用新定义的其他方法,但无法操作被子类隐藏的成员变量和方法,如果子类想使用被隐藏的方法或成员变量必须使用关键字super。
example5_4
package Example5_4;
class University{
void enterRule(double math,double english,double chinese){
double total=math+english+chinese;
if(total>=180)
System.out.println(total+"分数达到大学录取线");
else
System.out.println(total+"分数未达到大学录取线");
}
}
class ImprotantUniversity extends University{
void enterRule(double math,double english,double chinese) {
double total=math+english+chinese;
if(total>=220)
System.out.println(total+"分数达到重点大学录取线");
else
System.out.println(total+"分数未达到重点大学录取线");
}
}
public class Example5_4 {
public static void main(String[] args) {
double math=62,english=76.5,chinese=67;
ImprotantUniversity univer=new ImprotantUniversity();
univer.enterRule(math, english, chinese); //调用重写方法
math=91;
english=82;
chinese=86;
univer.enterRule(math, english, chinese); //调用重写方法
}
}
example5_5
package Example5_5;
class A{
float computer(float x,float y) {
return x+y;
}
public int g(int x,int y) {
return x+y;
}
}
class B extends A{
float computer(float x,float y) {
return x*y;
}
}
public class Example5_5 {
public static void main(String[] args) {
B b=new B();
double result=b.computer(8, 9); //b调用重写方法
System.out.println(result);
int m=b.g(12, 8); //b调用继承的方法
System.out.println(m);
}
}
9、重写方法的类型可以是父类方法类型的子类型,不必完全一致。
example5_6
package Example5_6;
class A{
Object get() {
return null; //返回空对象
}
}
class B extends A{
Integer get() { //Integer是Object的一个子对象
return new Integer(100); //返回一个Integer对象
}
}
public class Example5_6 {
public static void main(String[] args) {
B b=new B();
Integer t=b.get();
System.out.println(t.intValue());
}
}
10、▲重写父类方法时,不允许降低访问权限,但是可以提高。
11、用super操作被隐藏的成员变量和方法
super.x. super.play()
example5_7
package Example5_7;
class Sum{
int n;
float f() {
float sum=0;
for(int i=1;i<=n;i++)
sum=sum+i;
return sum;
}
}
class Average extends Sum{
int n;
float f() {
float c;
super.n=n;
c=super.f();
return c/n;
}
float g() {
float c;
c=super.f();
return c/2;
}
}
public class Example5_7 {
public static void main(String[] args) {
Average aver=new Average();
aver.n=100;
float resultOne=aver.f();
float resultTwo=aver.g();
System.out.println("resultOne="+resultOne);
System.out.println("resultTwo="+resultTwo);
}
}
12、子类在构造方法中需要使用super来调用父类的构造方法,super必须是子类构造方法的头一条语句。默认super()
example5_8
package Example5_8;
class Student{
int number;String name;
Student(){
}
Student(int number,String name){
this.number=number;
this.name=name;
System.out.println("我的名字是:"+name+"学号是:"+number);
}
}
class UniverStudent extends Student{
boolean 婚否;
UniverStudent(int number,String name,boolean b){
super(number,name);
婚否=b;
System.out.println("婚否="+婚否);
}
}
public class Example5_8 {
public static void main(String[] args) {
UniverStudent zhang=new UniverStudent(9901,"何晓林",false);
}
}
13、final关键字可以修饰类,成员变量和方法中的局部变量。final类不能被继承,不能有子类。
14、如果用final修饰父类中的一个方法,那么这个方法不允许子类重写,不允许子类隐藏可以继承的方法。
15、如果成员变量或局部变量被final修饰,那就是常量。由于常量在运行期间不允许再发生变化,所以常量在声明时没有默认值。
example5_9
package Example5_9;
class A{
final double PI=3.1415926; //PI是常量
public double getArea(final double r) {
return PI*r*r;
}
public final void speak() {
System.out.println("您好,How'severything here ?");
}
}
public class Example5_9 {
public static void main(String[] args) {
A a=new A();
System.out.println("面积:"+a.getArea(100));
a.speak();
}
}
代码调试过程中的问题
不熟悉代码原理和定义概念,不能很好的掌握
参考资料
《JAVA实用教材(第五版)》