创建两个类,分别为人类和地球类,人类中有两个方法 是吃和喝,地球类中有两个方法 是食物和水,结合两者,输出内容:1.“人类在吃地球上的食物”2.“人类在喝地球上的水”
<script type="text/javascript">
var earth = function () {
this.food = function () {
return '地球上的食物';
};
this.water = function () {
return '地球上的水';
};
};
var human = function () {
this.eat = function () {
return '人类在吃';
};
this.drink = function () {
return '人类在喝';
};
};
var myEarth = new earth;
var myHuman = new human;
document.write('1.' + myHuman.eat() + '' + myEarth.food());
document.write('<br/>');
document.write('2.' + myHuman.drink() + '' + myEarth.water());
</script>
public class Test {
static class Earth {
public String food(){
return "地球上的食物";
}
public String water(){
return "地球上的水";
}
}
static class Human {
private Earth earth;
public Human(Earth earth) {
this.earth = earth;
}
public String eat(){
return "人类在吃" + earth.food();
}
public String drink(){
return "人类在喝" + earth.water();
}
}
public static void main(String[] args) {
Earth earth = new Earth();
Human human = new Human(earth);
System.out.println(human.eat());
System.out.println(human.drink());
}
}
es6 Class了解一下。