**首先面向对象的三大特性:
1. 封装
2. 继承
3. 多态**
class Subclass extends Superclass{ }
class Person {
String name;
int age;
}
/*继承与Person类*/
class Student extends Person {
int weight;
}
class Person {
String name;
int age;
Person() {
System.out.println("Person的构造器!");
}
}
class Student extends Person {
Student() {
System.out.println("Student的构造器!");
}
int weight;
}
结果:
Person的构造器!
Student的构造器!
class Person {
String name;
int age;
public void say() {
System.out.println("I am person!");
}
}
class Student extends Person {
int weight;
public void say() {
System.out.println("I am student!");//方法重写
}
}
下面这个情况不是方法重写
class Person {
String name;
int age;
public void say() {
System.out.println("I am person!");
}
}
class Student extends Person {
int weight;
private void say() {
System.out.println("I am student!");//不是方法重写
}
}
原文地址:http://blog.csdn.net/ttf1993/article/details/45620997