简体   繁体   English

私有实例变量输入

[英]Private instance variables input

Im supposed to create a class called Farm having the following fields我应该创建一个名为 Farm 的类,具有以下字段

Add the following private instance variables:添加以下私有实例变量:

visitTime of type String字符串类型的访问时间

attendingCow of type Stable参加稳定型奶牛

horse of type Horse马类型马

visitTime of type String字符串类型的访问时间

I have to put private instance variables.我必须放置私有实例变量。

now i know how to do the strings, doubles etc of my issue how can I do a type of Stable, Horse?现在我知道如何处理我的问题的字符串、双打等了,我怎样才能做一种稳定的马? in which these are classes.其中这些是类。

private String visitTime;

question is how can i write a horse of type Horse (instance variable) Horse is a class in the same project.问题是我如何编写类型为Horse 的马(实例变量) Horse 是同一个项目中的一个类。

thats where im getting confused.这就是我感到困惑的地方。

You will have to define your own classes and then use them the way you use Strings or doubles.您必须定义自己的类,然后像使用字符串或双精度一样使用它们。 I have defined a class to represent horse (as of now i have only two proprties to horse which are name and age. You can add more).我已经定义了一个类来表示马(到目前为止,我只有两个马的属性,分别是名称和年龄。您可以添加更多)。 In the similar way you can define additional classes and use them the way I did in the HorseTest class below.以类似的方式,您可以定义其他类并像我在下面的 HorseTest 类中所做的那样使用它们。 Go throughhttp://docs.oracle.com/javase/tutorial/java/javaOO/classes.html通过http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Class Horse
{
   private String name;
   private int age;
   public Horse(String aName, int aAge)
   {
       name = aName;
       age = aAge;
   }

   public String getName()
   {
       return name;
   }

   public int getAge()
   {
       return age;
   }
}


Class HorseTest
{
   public static void main(String args[])
   {
       Horse h1 = new Horse("Ginger", 4);
       Horse h2 = new Horse("Black Beauty", 3);

       System.out.println(h1.getName());
       System.out.println(h2.getName());
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM