继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
一个简单地继承类程序:
class Student extends Worker
{
String name = "zhangsan";
int age = 21;
void study()
{
System.out.println(super.name);
}
}
class Worker
{
String name = "lisi";
int age = 43;
void work()
{
System.out.println("work good");
}
}
class Jicheng
{
public static void main(String[] args)
{
Student s = new Student();
s.study();
Worker w = new Worker();
w.work();
Fu z = new Fu(4);
System.out.println(z.num);
System.out.println(s.name+"..."+w.name);
}
}
class Fu
{
int num ;
Fu(int x)
{
num = 60;
System.out.println("fu run = "+x);
}
}
一个简单地继承类程序:
class Student extends Worker
{
String name = "zhangsan";
int age = 21;
void study()
{
System.out.println(super.name);
}
}
class Worker
{
String name = "lisi";
int age = 43;
void work()
{
System.out.println("work good");
}
}
class Jicheng
{
public static void main(String[] args)
{
Student s = new Student();
s.study();
Worker w = new Worker();
w.work();
Fu z = new Fu(4);
System.out.println(z.num);
System.out.println(s.name+"..."+w.name);
}
}
class Fu
{
int num ;
Fu(int x)
{
num = 60;
System.out.println("fu run = "+x);
}
}