一、学生信息
class Student {
String Name;
int Age;
String Degree;
Student(String n,int a,String d) {
Name=n;
Age=a;
Degree=d;
}
}
class Undergraduate extends Student {
String Professional;
Undergraduate(String n,int a,String d,String p) {
super(n,a,d);
Professional=p;
}
void show() {
System.out.println("本科生:\t姓名:"+Name+"\t年龄:"+Age+"\t学位:"+Degree+"\t专业:"+Professional);
}
}
class Master extends Student {
String Direction;
Master(String n,int a,String d,String dir) {
super(n,a,d);
Direction=dir;
}
void show() {
System.out.println("研究生:\t姓名:"+Name+"\t年龄:"+Age+"\t学位:"+Degree+"\t研究方向:"+Direction);
}
}
public class MyStudent {
public static void main(String args[]) {
Undergraduate undergraduate=new Undergraduate("张三",20,"学士","软件工程");
Master master=new Master("李四",25,"硕士","网络工程");
undergraduate.show();
master.show();
}
}
运行结果:
二、车辆信息
class Car {
int Number;
int Weight;
Car(int n,int w){
Number=n;
Weight=w;
}
}
class Truck extends Car {
int Load;
Truck(int n, int w,int l) {
super(n, w);
Load=l;
}
void show() {
System.out.println("卡车:\t车轮个数:"+Number+"\t车重:"+Weight+"\t载重量:"+Load);
}
}
class Van extends Car{
int Busload;
Van(int n, int w,int b) {
super(n, w);
Busload=b;
}
void show() {
System.out.println("面包车:\t车轮个数:"+Number+"\t车重:"+Weight+"\t载客量:"+Busload);
}
}
public class MyCar {
public static void main(String args[]) {
Truck truck=new Truck(16,2000,5000);
Van van=new Van(8,1000,12);
truck.show();
van.show();
}
}
运行结果:
三、圆的体积和面积
class Circle {
public static final double PI=3.14;
int X;
int Y;
double R;
Circle(int x,int y,double r) {
X=x;
Y=y;
R=r;
}
int getX() {
return X;
}
int getY() {
return Y;
}
double getR() {
return R;
}
void perimeter() {
double l;
l=2*PI*R;
System.out.println("周长:"+l);
}
void area() {
double a;
a=PI*R*R;
System.out.println("面积:"+a);
}
}
class Cylinder extends Circle {
Cylinder(int x, int y, double r) {
super(x, y, r);
}
double High;
void sethigh(double h) {
High=h;
}
double getHigh() {
return High;
}
void area(){
double a;
a=2*PI*R*R+2*PI*R*High;
System.out.println("表面积:"+a);
}
void volume(){
double v;
v=PI*R*R*High;
System.out.print("体积:"+v);
}
}
public class MyCircle {
public static void main(String args[]) {
Cylinder cylinder=new Cylinder(0,0,2);
cylinder.sethigh(2);
System.out.println("圆柱体信息:\n坐标:("+cylinder.X+","+cylinder.Y+")\t底面半径:"+cylinder.R+"\t高度:"+cylinder.High);
cylinder.area();
cylinder.volume();
}
}
运行结果: