package l3c3computerscience;
public class L3C3ComputerScience {
public static void main(String[] args)
{
Cat myCat = new Cat();
Dog myDog = new Dog();
myCat.createCat(); //error is here
myDog.createDog(); //error is also here
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package l3c3computerscience;
import java.util.Scanner;
/**
*
* @author
*/
public class Mammal {
public Scanner input;
public Dog myDog;
public Cat myCat;
public void createCat()
{
input = new Scanner(System.in);
myCat = new Cat();
String name;
System.out.println("What is this cat's name?");
name = input.next();
myCat.setName(name);
String race;
System.out.println("What is this cat's race?");
race = input.next();
myCat.setRace(race);
}
public void createDog()
{
input = new Scanner(System.in);
myDog = new Dog();
String name;
System.out.println("What is this dog's name?");
name = input.next();
myDog.setName(name);
String race;
System.out.println("What is this dog's race?");
race = input.next();
myDog.setRace(race);
}
public void display()
{
System.out.println("I have a "+myCat.getRace()+" cat and its name is "+myCat.getName());
System.out.println("I have a "+myDog.getRace()+" dog and its name is "+myDog.getName());
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package l3c3computerscience;
/**
*
* @author Xander
*/
public class Cat
{
private String name;
private String race;
public Cat()
{
this.name = name;
this.race = race;
}
public void setName(String name)
{
this.name = name;
}
public void setRace(String race)
{
this.race = race;
}
public String getName()
{
return this.name;
}
public String getRace()
{
return this.race;
}
public void display()
{
System.out.println("The cat's name is" + name);
System.out.println("The cat's race is" + race);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package l3c3computerscience;
/**
*
*/
public class Dog
{
private String name;
private String race;
public Dog()
{
this.name = name;
this.race = race;
}
public void setName(String name)
{
this.name = name;
}
public void setRace(String race)
{
this.race = race;
}
public String getName()
{
return name;
}
public String getRace()
{
return race;
}
public void display()
{
System.out.println("The dog's name is" + name);
System.out.println("The dog's race is" + race);
}
}
它不断给我,在第一部分下找不到符号。 “createCat /狗();”有下划线说“找不到符号”无法弄清楚原因,:(请帮帮我......
答案 0 :(得分:1)
我不想这么说,但是你当前的程序设计已经关闭了:
this.name = name;
等调用的构造函数会将字段设置为null
值,因为这些构造函数没有参数。e.g。
public Dog(String name, String race) {
this.name = name;
this.race = race;
}
void
方法,而是分别返回Cat或Dog对象。 如,
public static Dog createDog(Scanner input) {
// code to interact with user and create a dog
String name = // ..... use input to get name from user
String race = // ..... use input to get race from user
// use the new constructor you've created that takes parameters
Dog dog = new Dog(name, race);
// return the Dog from the method to the caller
return dog;
}
同样适用于createCat
答案 1 :(得分:-1)
根据您的要求:
首先,您需要在Mammal
方法中创建main
类对象,因为createCat()
和createDog()
方法属于Mammal
类。
其次,你将使用Mammal
类,并且它位于。{
名为l3c3computerscience
的包,因此导入l3c3computerscience.Mammal
;
最后,您的L3C3ComputerScience.java
文件如下所示:
import l3c3computerscience.Mammal;
public class L3C3ComputerScience {
public static void main(String[] args) {
Mammal mammal = new Mammal();
mammal.createCat();
mammal.createDog();
mammal.display();
}
}
按顺序运行:
javac -d . Cat.java
javac -d . Dog.java
javac -d . Mammal.java
javac L3C3ComputerScience.java
java L3C3ComputerScience
希望,它将满足您的要求。