以下内容来自marschen的java4android视频教程.
第18集 继承的初步.
1.什么是继承.2.为什么要使用继承.
3.继承的基本语法特点.
在面向对象的世界当中,继承就是一个类得到了另外一个类当中的成员\
变量和成员方法.
java当中只支持单继承,不允许多继承.
一个子类只能继承一个父类,
但在C++里面一个子类可以有多个父类.
关键字extends. 扩展.
面对对象的终极目标是减少应该程序的重复代码.
其实继承一方面就是为了减少重复的代码.
总结:
1.使用继承是为了减少重复代码.
2.继承的基本语法.带有点扩展的意思在里面.
class Person{
String name;
int age;
void eat(){
System.out.println("吃饭");
}
void introduce(){
System.out.println("我的名字是" + name + "年龄" + age);
}
}
class Student extends Person{
int grade;
void study(){
System.out.println("学习");
}
}
class TestStu{
public static void main(String args[])
{
Student student = new Student();
student.name = " 张三 ";
student.age = 15;
student.grade = 3;
student.eat();
student.introduce();
student.study();
}
}