简单的Java代码实现二

理解类的继承性

1.学生信息

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();
	}
}

在这里插入图片描述
2.车轮数量

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();
	}
}

在这里插入图片描述
3.圆柱体

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();
	}
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值