我对java继承有相当基本的了解。有人能解释一下应该怎么做吗?
这个问题是在我最近的编码回合中提出的 我必须打印以下行: My name is Fastfood. My name is Fruits. Our superclass is Food I'm serving Fastfood I'm serving Fruit我对java继承有相当基本的了解。有人能解释一下应该怎么做吗?,java,inheritance,Java,Inheritance,这个问题是在我最近的编码回合中提出的 我必须打印以下行: My name is Fastfood. My name is Fruits. Our superclass is Food I'm serving Fastfood I'm serving Fruit 我试了很多次,经过一个小小的修改,我得到了解决方案 class FoodFactory extends Solution { public Food getFood(String string) { r
我试了很多次,经过一个小小的修改,我得到了解决方案
class FoodFactory extends Solution
{
public Food getFood(String string)
{
return new Food(string);
}
}
class Food extends Solution
{
String name=null;
public Food(String string)
{
this.name=string;
}
public void servesFood()
{
System.out.println("I'm serving "+this.name);
}
}
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.name);
System.out.println("My name is: " + food2.name);
System.out.println("Our superclass is: " + food1.getClass().getName());//modification
food1.servesFood();
food2.servesFood();
}}
我已经测试了它,它的工作如预期
`class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit("Fruit");
} else {
return new FastFood("FastFood");
}
}
}
class Fruit extends Food {
public Fruit(String name1) {
super.name = name1;
}
}
class FastFood extends Food {
public FastFood(String name1) {
super.name = name1;
}
}
class Food {
public String name = null;
public Food() {
}
public Food(String string) {
this.name = string;
}
public void servesFood() {
System.out.println("I'm serving " + this.name);
}
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}`
与给出的答案相似,但带有抽象食物类
class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit();
} else if (string.equals("FastFood"){
return new FastFood();
}
return null;
}
@Override
public void servesFood() {
}
}
class Fruit extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
class FastFood extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
abstract class Food {
public abstract void servesFood();
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}
我检查了答案,但我检查了这个结果,注意目标是所有参数的复合短语。在许多情况下,答案必然是在标记的摘录之后,而不是像我下面描述的那样使用整个类 编译成功
/*
* My name is: Fastfood
* My name is: Fruits
* Our superclass is: Food
* I'm serving Fastfood
* I'm serving Fruits
*/
class Solution {
public static void main(String[] argh) {
foodFactory myFoods = new foodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: " + food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
// attention: from here you will have the correct use
class foodFactory extends Solution {
public Food getFood(String food) {
if (food.equals("Fastfood")) {
return new Fastfood();
} else {
return new Fruits();
}
}
}
class Food extends Solution {
public void serveFood() {
System.out.println("I'm serving " + this.getClass().getName());
}
}
class Fruits extends Food {
}
class Fastfood extends Food {
}
你是在问我们如何编写食物/快餐/水果课程吗?@NikG我正要问同样的问题xD;)什么是
解决方案
类?你能给我们看一下你的食品
、水果
和快餐
课程吗?@NikG你为什么要写水果课???@sinisteraadi这听起来像是一个仓促编写的课堂黑板写作示例/作业出错了。如果您已经对继承有了一些了解,那么一定要询问新的或笨拙的代码。我特别为为什么所有的东西都扩展了解决方案而烦恼。食品工厂没有理由继承食品。它不是食物,它是某种工厂。我们继承它是因为我们希望我们的超类被打印为“食物”,它是FoodFactory,所以它应该打印FoodFactory。
`class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit("Fruit");
} else {
return new FastFood("FastFood");
}
}
}
class Fruit extends Food {
public Fruit(String name1) {
super.name = name1;
}
}
class FastFood extends Food {
public FastFood(String name1) {
super.name = name1;
}
}
class Food {
public String name = null;
public Food() {
}
public Food(String string) {
this.name = string;
}
public void servesFood() {
System.out.println("I'm serving " + this.name);
}
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}`
class FoodFactory extends Food {
public Food getFood(String string) {
if (string.equals("Fruit")) {
return new Fruit();
} else if (string.equals("FastFood"){
return new FastFood();
}
return null;
}
@Override
public void servesFood() {
}
}
class Fruit extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
class FastFood extends Food {
@Override
public void servesFood() {
System.out.println("I'm serving " + getClass().getName());
}
}
abstract class Food {
public abstract void servesFood();
}
class Solution1 {
public static void main(String[] args) throws java.lang.Exception {
FoodFactory myFoods = new FoodFactory();
Food food1 = myFoods.getFood("FastFood");
Food food2 = myFoods.getFood("Fruit");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: "
+ food1.getClass().getSuperclass().getName());// modification
food1.servesFood();
food2.servesFood();
}
}
/*
* My name is: Fastfood
* My name is: Fruits
* Our superclass is: Food
* I'm serving Fastfood
* I'm serving Fruits
*/
class Solution {
public static void main(String[] argh) {
foodFactory myFoods = new foodFactory();
Food food1 = myFoods.getFood("Fastfood");
Food food2 = myFoods.getFood("Fruits");
System.out.println("My name is: " + food1.getClass().getName());
System.out.println("My name is: " + food2.getClass().getName());
System.out.println("Our superclass is: " + food1.getClass().getSuperclass().getName());
food1.serveFood();
food2.serveFood();
}
}
// attention: from here you will have the correct use
class foodFactory extends Solution {
public Food getFood(String food) {
if (food.equals("Fastfood")) {
return new Fastfood();
} else {
return new Fruits();
}
}
}
class Food extends Solution {
public void serveFood() {
System.out.println("I'm serving " + this.getClass().getName());
}
}
class Fruits extends Food {
}
class Fastfood extends Food {
}