标签:style blog http color ar 使用 java sp div
什么是继承?
继承就是一个类得到了另外一个类的成员变量和成员方法。Java只支持单继承,一个子类只允许继承一个父类,不允许多继承。使用继承是为了减少类定义时的重复代码。

继承的基本语法特点:
class 子类 extends 父类
class Person{ //父类
String name;
int age;
void introduce(){
System.out.println("我的名字是" + name + ",我的年龄是" + age);
}
}
class Student extends Person{ //继承父类Person
public static void main(String args[]){
Student s1 = new Student();
s1.name = "张三";
s1.age = 18;
s1.introduce();
}
}
子类Student继承了父类Person后就拥有了父类的成员变量和成员方法,与此同时在子类中还可以定义子类特有的成员变量和成员函数。
标签:style blog http color ar 使用 java sp div
原文地址:http://www.cnblogs.com/chavez-wang/p/4051146.html