/*JAVA的继承、构造、重载、重写其实就是和c++很类似,可以这样说java的的继承、构造、重载、重写就是继承于C++,所谓了重载和重写就是多态,对于多态,在C++用virtual来实现,重载、重写的本质去被就在于重载是在同一类中,重写是在父类和子类中发生的,而多态是面向对象编程的一主要特点,看他是不是面向对象就看他是不是支持多态,故 VB 就不是面向对象的编程语言,因为他不支持多态,可VB又有面向对象其他的特点,所以我们称类似VB编程语言叫基于对象的编程。
JAVA的继承、构造、重载、重写
/**
* Student.java
* version1.0 2006-9-25
* write by jsp 028-80889574
* 此程序用于演示类的继承、构造、重载、重写;
*/
public class Student
{
String name;
String stuNo;
int age;
int cost;
Student(String name, String StuNo,int age,int cost)
{
this.name = name;
this.stuNo = StuNo;
this.age = age;
this.cost = cost;
}
Student(String name, String StuNo,int age)
{
this.name = name;
this.stuNo = StuNo;
this.age = age;
}
int costtotal()
{
return cost;
}
int costtotal(int i)
{
return 0;
}
public static void main(String[] args)
{
Student stu1 = new Student("jsp","960515",25,320);
System.out.println("姓名/t学号/t年纪/t学费/t住宿费");
System.out.println(stu1.name+"/t"+stu1.stuNo+"/t"+stu1.age+"/t"+stu1.cost+"/t"+"0");
System.out.println(stu1.name+"的费用是:"+stu1.costtotal());
System.out.println("===========================================");
MidStudent midstu2 = new MidStudent("jsg","960517",25,320,180);
System.out.println("姓名/t学号/t年纪/t学费/t住宿费");
System.out.println(midstu2.name+"/t"+midstu2.stuNo+"/t"+midstu2.age+"/t"+midstu2.cost+"/t"+midstu2.stayCost);
System.out.println(midstu2.name+"的费用是:"+midstu2.costtotal());
System.out.println("===========================================");
MidStudent midstu3 = new MidStudent("wqj","960518",20);
System.out.println("姓名/t学号/t年纪/t学费/t住宿费");
System.out.println(midstu3.name+"/t"+midstu3.stuNo+"/t"+midstu3.age+"/t"+"0"+"/t"+"0");
System.out.println(midstu3.name+"学生的费用是:"+midstu3.costtotal(0)+"/t此学生是免费的!");
}
}
class MidStudent extends Student
{
int stayCost;
MidStudent(String name, String stuNo,int age,int cost,int stayCost)
{
super(name,stuNo,age,cost);
this.stayCost = stayCost;
}
MidStudent(String name, String stuNo,int age)
{
super(name,stuNo,age);
}
int costtotal()
{
return cost+stayCost;
}
}
C++ 见C++部分(此处略)