一个综合的类,这个类里面有就是要求啊,比方说学生累,那你这个类里面里面又有构造方法,重载方法,有继承,有重写,有接口,那么里边的是显示什么内容是大家自己设计。
植物:
//测试类
public class Dome3 {
public static void main(String[] args) {
//创建对象,调用方法
rose r=new rose();
//调用继承与实现过来的方法
r.describe();
r.col();
System.out.println("----------------------");
//无参构造赋值
r.setKind("红玫瑰");
System.out.println("品种1:"+r.getKind());
//带参构造赋值
r=new rose("白玫瑰");
System.out.println("品种2:"+r.getKind());
System.out.println("******************************************");
}
}
/**定义抽象的植物类*/
abstract class Plant {
//定义成员变量
private String kind; //植物种类
//生成无参构造方法
public Plant() {
}
//生成带参构造方法
public Plant(String kind) {
this.kind = kind;
}
//生成get和set方法
public String getKind(){
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
//添加一个描述植物的抽象方法
public abstract void describe();
}
/**定义接口Color颜色*/
interface Color {
//定义抽象方法color
public abstract void col();
}
/**这里是Rose继承了Plant 父类并实现了Color接口*/
class rose extends Plant implements Color {
//重写describe()和col()方法,这里就是方法的重载
@Override
public void describe(){
System.out.println("描述:玫瑰花朴素大方,花瓣层层叠叠,微微下卷,在阳光的照耀下,花瓣犹如涂上了一层明油,光泽而明亮。");
}
@Override
public void col(){
System.out.println("颜色:玫瑰花的花色主要为红与白。");
}
//生成无参和带参数的构造方法
public rose() {
}
public rose(String kind) {
super(kind);
}
}
电器:
//测试类
public class Dome4 {
public static void main(String[] args) {
//创建对象,调用方法
Cooker c=new Cooker();
//调用继承与实现过来的方法
c.describeEle();
c.place();
System.out.println("----------------------");
//无参构造赋值
c.setElekind( "多功能电饭锅");
c.setPrice(340);
System.out.println("种类1:"+c.getElekind()+"价格:"+c.getPrice());
//带参构造赋值
c=new Cooker("迷你电饭锅",120);
System.out.println("种类2:"+c.getElekind()+"价格:"+c.getPrice());
System.out.println("******************************************");
}
}
/**定义抽象的电器父类*/
abstract class ElectricEquipment {
//定义成员变量
private String elekind; //电器种类
private int price; //电器价格
//生成带参构造函数
public ElectricEquipment(String elekind, int price) {
this.elekind = elekind;
this.price = price;
}
//生成无参构造函数
public ElectricEquipment() {
}
//生成set和get方法
public String getElekind() {
return elekind;
}
public void setElekind(String elekind) {
this.elekind = elekind;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
//添加一个描述电器的抽象方法
public abstract void describeEle();
}
/**定义接口place_of_origin产地*/
interface place_of_origin {
//定义抽象方法生产地
public abstract void place();
}
/**这里是Cooker电饭锅继承了ElectricEquipment 父类并实现了place_of_origin 接口*/
class Cooker extends ElectricEquipment implements place_of_origin {
//重写describe()和col()方法,这里就是方法的重载
@Override
public void describeEle(){
System.out.println("电饭锅是一种能够进行蒸、煮、炖、煨、焖等多种加工的现代化炊具。");
}
@Override
public void place(){
System.out.println("产于中国");
}
//生成无参和带参数的构造方法
public Cooker(String elekind, int price) {
super(elekind, price);
}
public Cooker() {
}
}
普通学生类:
public class Student {
//学生姓名
private String studentName;
//学生学号
private String studentNo;
public String getStudentName() {
return studentName;
}
public Student(String studentName, String studentNo) {
super();
this.studentName = studentName;
this.studentNo = studentNo;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public void showSelf(String studentName, String studentNo) {
System.out.println("student: studentName=" + studentName + ",studentNo=" + studentNo );
}
}
普通接口:
/**
* 接口,用于显示自己的名字
*/
public interface Person {
void showName(String name);
}
综合类
/**
* 大学生类,继承学生类,实现person接口
*
*/
public class CollegeStudent extends Student implements Person{
//所在大学
private String collegeName;
//构造方法
public CollegeStudent(String studentName, String studentNo, String collegeName) {
super(studentName, studentNo);
this.collegeName = collegeName;
}
//实现接口
@Override
public void showName(String name) {
System.out.println(name);
}
//重载方法
public void showSelf(String studentName, String studentNo, String collegeName) {
System.out.println("student: studentName=" + studentName + ",studentNo=" + studentNo + ",collegeName=" + collegeName);
}
}
无
这不是一个典型的作业吗,连具体要求都没有,说白了你把书上例题抄下来,类名改一下不就得了
那就可以直接写了!
接口,类,
interface YunDongYuan {
void run();
}
class People {
private String name;
public People(String name) {
this.name = name;
}
public void eat() {
System.out.println("没有食物吃");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* 实现接口YunDongYuan
* 继承People
*/
public class Student extends People implements YunDongYuan {
private String schoolName;
/**
* 无参数构造方法
*/
public Student() {
super(null);
}
/**
* 构造方法
*
* @param name
* @param schoolName
*/
public Student(String name, String schoolName) {
super(name);
this.schoolName = schoolName;
}
/**
* 实现接口方法
*/
@Override
public void run() {
System.out.println(this.schoolName + "我是学生跑的不是那么快");
}
/**
* 方法重写
*/
@Override
public void eat() {
System.out.println(this.schoolName + "没有食物吃");
}
/**
* 方法重载
*
* @param food
*/
public void eat(String food) {
System.out.println(this.schoolName + "有食物" + food);
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public static void main(String[] args) {
final Student student = new Student("张三", "清华大学");
student.eat();
student.eat("鸡蛋");
student.run();
}
}
//接口
interface studentImpl {
void learningStyle();
}
class studentSup {
protected String features() {
return "高大威猛";
}
}
//继承 studentSup
class student extends studentSup implements studentImpl {
//构造函数
void student() {
}
//重载方法
public String features() {
return "矮小瘦弱";
}
//实现接口
@Override
public void learningStyle() {
System.out.println("努力学习");
}
}