黑马程序员—java技术blog—第五篇面向对象_继承概述及基础应用

来源:互联网 发布:mac加拿大官网 编辑:程序博客网 时间:2024/05/26 05:51
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
 1. 有很多类,但是这些类有很多相同的部分,把这些重复东西向上抽取,封装成一个类,把这个类叫做父类. 很多类叫做子类 .
子类继承父类就可以了,那么重复的部分就再也不用写了,直接可以使用.
发展过程: 先有子类,再抽取父类
编程过程: 先写父类,再使用继承 
2.A:继承的好处
a:提高了代码的复用性
b:提高了代码的维护性
修改父类,子类的都不用修改 
c:让类与类之间产生了关系(父子关系),是多态的前提
3.B:继承的弊端
关系太近也不好.设计类的时候,把握以下关系远近程度
原则:高内聚 ,低耦合  
高内聚: 自己能干从来不求别人 
低耦合: 有关系,但是别太近
4.注意事项: 
1.父类私有的,子类也继承不到(私有的东西,儿子也不给)
2.构造方法不能继承
3.不能随便的使用继承,属性,方法.
使用继承的情况,满足一种关系 
子类 "is a " 父类 ,才能使用继承
例:
class FruitTest{
public static void main(String[] args){
//创建子类对象  
Apple a = new Apple("red",2.0);
System.out.println(a.getColor());
a.toFood();
a.beautiful();
}
}
lass Fruit {
//重复的抽取出来 
//成员变量 
private String color;
double weight;
//构造方法 
Fruit(){}


Fruit(String color,double weight){
this.color = color;
this.weight = weight; 
}
//成员方法
//get /set
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public double getWeight(){
return weight;
}
public void setWeight(double weight){
this.weight = weight;
}
//特有
//toFood(){}  //作为食物
public void toFood(){
System.out.println("吃水果 ,就得吃应季的 ");
}


//toJuice(){}  //榨成水果汁
public void toJuice(){
System.out.println("榨成果汁 还是 很nice的  ");
}
public void toMoney(){}
}
class Apple extends Fruit {
//构造方法 
Apple(){}
Apple(String color,double weight){
// this.color = color;
// this.weight = weight; 
super(color,weight);
}
//美白
public void beautiful(){
//System.out.println( super.color + "吃苹果,据说是美白...");
System.out.println( getColor()+ "吃苹果,据说是美白...");
}
}
class Watermelon  extends Fruit {
//构造方法 
Watermelon(){}
Watermelon(String color,double weight){
//this.color = color;
this.weight = weight; 
}
//解暑
public void cool(){
System.out.println("吃西瓜解暑,凉快,别吃多了....");
}
}

另注:当子类和父类有重名的方法时,运行子类的方法,即方法重写
            当成员变量是静态时,要使用的方法也必须是静态方法
 二>   final关键字
        当final修饰类时,那这个类不可作为父类,即不能被继承
        当final修饰方法时,这个方法就不能被重写
        当final修饰成员变量时,这个变量只能赋值一次,即编程了常量。
三》方法重写的注意事项:


1.父类私有的不能重写
2.子类重写父类方法,权限不能更低
3.静态的方法必须使用静态的方法来重写


子类重写父类方法的时候,最好声明一模一样。




方法重写的面试题
Override和Overload的区别?
重写和重载的区别?

注意事项: 
1.子类重写父类的方法,权限不能更低. 高一点没有关系,但是建议保证一模一样
2.父类私有的,子类不能重写 

class  PhoneTest{

public static void main(String[] args){
NewPhone np = new NewPhone();
np.playMusic();


np.send();
NewPhone.send();
}
}
class Phone{
   private void call(){
System.out.println("打电话...");
}
void playMusic(){
System.out.println("听音乐....");
System.out.println("定时关机");
}
public static void send(){
System.out.println("发短信");
}
}
class NewPhone extends Phone{


// private void call(){
// System.out.println("打网络电话...");
// }


public void playMusic(){
System.out.println("根据你的情绪,推荐音乐");
super.playMusic();  //调用父类成员 

}


public static void send(){   //错误:NewPhone中的send()无法覆盖Phone中的send()
System.out.println("发网络短信,走流量....");
}

}
final案例:
final :最终的意思 


修饰 
类:不能被继承 (例如 : String )
成员变量:变量就变成了常量,只能被赋值一次(定义这个成员变量就给值)
自定义常量: 
final int x =250;  
Final String URL = "192.168.99.56:8080/waimai" 

成员方法:不能被重写 




被final修饰的东西 ,不能被改变


*/


class  FinalDemo{


public static void main(String[] args){
Zi z = new Zi();
System.out.println(z.num);
//z.num =20;   //错误: 无法为最终变量num分配值
//System.out.println(z.num);


//System.out.println(z.num2);




z.show();
}
}


//final class  Fu{   //错误: 无法从最终Fu进行继承
class  Fu{ 
final int num =10;
//final int num2 ;   // 错误: 可能尚未初始化变量num2


public final  void show(){
System.out.println("fu的num" + num);
}


}


class  Zi extends  Fu{


public void show(){    // 错误: Zi中的show()无法覆盖Fu中的show()
System.out.println("zi的num" + num);
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------





0 0