作用:实现对程序的反复使用,提高程序的维护性。
关键字extends
格式:
【修饰符】 class 子类名 extends 父类名
使用原则:
子类可以继承父类中所有可被子类访问的成员变量与成员方法(public protected);
使用this关键字
当局部变量和成员变量的名字相同时,成员变量就会被隐藏,这时如果想在成员方法中使用成员变量,就必须用this关键字
语法格式:
this.成员变量名
this.成员方法名()
public class Fruit
{
public String color = "";
public void harvest(){
String color=“”;
System.out.println();
System.out.println();
System.out.println(this.color);
}
public static void main(String【】args)
{
Fruit obj =new Fruit();
obj.harvest();
}
}