创建一个大象的class
创建冰箱和大象的对象,调用冰箱的三个方法将大象装进去。。。
开门----->放大象--------->关门
然后没了。
package know.t3import java.util.ArrayList
import java.util.List
/**
* 大象
* @author 42225_000
*
*/
public class Elephant {
private Object head//头
private List<Object> feets//四肢
private Object body//躯干
private Object tail//尾巴
public Elephant(){
head=new Object()
feets=new ArrayList<Object>()
feets.add(new Object())
feets.add(new Object())
feets.add(new Object())
feets.add(new Object())
body=new Object()
tail=new Object()
}
public Object getHead() {
return head
}
public void setHead(Object head) {
this.head = head
}
public List<Object> getFeets() {
return feets
}
public void setFeets(List<Object> feets) {
this.feets = feets
}
public Object getBody() {
return body
}
public void setBody(Object body) {
this.body = body
}
public Object getTail() {
return tail
}
public void setTail(Object tail) {
this.tail = tail
}
public String toString(){
return "大象"
}
}
package know.t3
/**
* 冰箱
* @author 42225_000
*
*/
public class Refrigerator {
public void open(){
System.out.println("打开冰箱门")
}
public void put(Object obj){
System.out.println("把"+obj.toString()+"放进冰箱")
}
public void close(){
System.out.println("关上冰箱门")
}
}
package know.t3
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//牵一头大象
Elephant e=new Elephant()
//准备一个冰箱
Refrigerator r=new Refrigerator()
r.open()
r.put(e)
r.close()
}
}
具体的自己参考,以定义学生类为例package js.topcom
public class Student {
//属性包括姓名,学号,语文成绩,数学成绩,英语成绩
private String name
private String id
private int chn_score
private int math_score
private int en_score
//方法包括输出数据,设置姓名和学号,设置三门课程的成绩,求总成绩和平均成绩
public void diplay(){
System.out.println("the student name is: " + this.name)
System.out.println("the student id is: " + this.id)
System.out.println("the student chinese score is: " + this.chn_score)
System.out.println("the student math score is: " + this.math_score)
System.out.println("the student english score is: " + this.en_score)
System.out.println("the student total score is: " + this.get_total_score())
System.out.println("the student average score is: " + this.get_avg_score())
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getId() {
return id
}
public void setId(String id) {
this.id = id
}
public int getChn_score() {
return chn_score
}
public void setChn_score(int chnScore) {
chn_score = chnScore
}
public int getMath_score() {
return math_score
}
public void setMath_score(int mathScore) {
math_score = mathScore
}
public int getEn_score() {
return en_score
}
public void setEn_score(int enScore) {
en_score = enScore
}
public int get_total_score(){
return this.chn_score + this.en_score + this.math_score
}
public int get_avg_score(){
return this.get_total_score() / 3
}
public static void main(String[] args) {
Student stu = new Student()
stu.setName("baidu")
stu.setId("123")
stu.setChn_score(89)
stu.setEn_score(98)
stu.setMath_score(88)
stu.diplay()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)