实验任务三(面向对象基础一)
Java的课堂小实验报告,放上来水一下
可以帮助一些刚入门不想写类似实验报告的人(误)
- 设计一个汽车类Vehicle,包含的成员属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含属性载人数。卡车Truck是Vehicle的子类,其中包含载人数和载重量payload。每个类都有相关数据的输出方法。
代码:
class Vehicle{
int wheel,weight;
protected Vehicle(int wheel,int weight){
this.wheel=wheel;
this.weight=weight;
}
protected Vehicle(){
}
void setwheel(int wheel){
this.wheel=wheel;
}
void displaywheel(){
System.out.println("车辆的轮子为:"+wheel);
}
void setweight(int weight){
this.weight=weight;
}
void displayweight(){
System.out.println("车辆的重量为:"+weight);
}
}
class car extends Vehicle{
public car(int wheel,int weight,int number){
super(wheel,weight);
this.number=number;
}
public car(){
}
int number;
void setNum(int number){
this.number=number;
}
void displayNum(){
System.out.println("小车的载车人数为:"+number);
}
}
class truck extends Vehicle{
int number,payload;
public truck(int wheel,int weight,int number){
super(wheel,weight);
this.number=number;
}
public truck(int wheel,int weight,int number,int payload){
super(wheel,weight);
this.number=number;
this.payload=payload;
}
public truck(){
}
void setNum(int number){
this.number=number;
}
void displayNum(){
System.out.println("卡车的载车人数为:"+number);
}
void setpayload(int payload){
this.payload=payload;
}
void displaypayload(){
System.out.println("卡车的载重量为:"+payload);
}
}
public class VehicleDemo {
public static void main(String[] args) {
car benz=new car();
benz.setweight(100);
benz.setwheel(4);
benz.setNum(5);
benz.displayweight();
benz.displaywheel();
benz.displayNum();
System.out.println("----------------------------");
truck moto=new truck(4,200,3,100);
moto.displayweight();
moto.displaywheel();
moto.displayNum();
moto.displaypayload();
}
}
运行结果:

- 设计一个学生类Student,包含的属性有姓名name和年龄age.由学生类派生出本科生类Undergraduate和Graduate,本科生类包含的属性有专业specialty,研究生类包含的属性有研究方向studydirection.每个类都有相关的数据输出方法。(用上this,super关键字)
代码:
class student{
int age;
String name;
protected student(String name,int age){
this.name=name;
this.age=age;
}
protected student(){
}
void setage(int age){
this.age=age;
}
void displayage(){
System.out.println("年龄:"+age);
}
void setname(String name){
this.name=name;
}
void displayname(){
System.out.println("学生姓名:"+name);
}
}
class undergraduate extends student{
String specialty;
public undergraduate(String name,int age,String specialty){
super(name,age);
this.specialty=specialty;
}
public undergraduate(){
}
void setspecialty(String specialty){
this.specialty=specialty;
}
void displayspecialty(){
System.out.println("专业方向:"+specialty);
}
}
class graduate extends student{
String studydirection;
public graduate(String name,int age,String studydirection){
super(name,age);
this.studydirection=studydirection;
}
public graduate(){
}
void setstudydirection(String studydirection){
this.studydirection=studydirection;
}
void displaystudydirection (){
System.out.println("研究方向:"+studydirection);
}
}
public class StudentDemo {
public static void main(String[] args) {
graduate dl=new graduate("",18,"math");
dl.displayname();
dl.displayage();
dl.displaystudydirection ();
System.out.println("-------------下面为本科生--------------");
undergraduate jr=new undergraduate("",18,"math");
jr.displayname();
jr.displayage();
jr.displayspecialty();
}
}
实验结果:

这篇博客是关于Java面向对象基础的实验报告,包括设计Vehicle、Car、Truck类及其属性与方法,以及Student、Undergraduate、Graduate类的继承结构。通过实例展示了类的设计和继承的概念。
263

被折叠的 条评论
为什么被折叠?



