在Java中访问类的字段
我对Java完全陌生 我在练习一个关于一个人吃水果的规则。我有三节课 水果类:在Java中访问类的字段,java,oop,Java,Oop,我对Java完全陌生 我在练习一个关于一个人吃水果的规则。我有三节课 水果类: public class Fruit { String fruitname = "grapes"; } public class Person { void eat(Fruit f) { System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname } } public
public class Fruit {
String fruitname = "grapes";
}
public class Person {
void eat(Fruit f) {
System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
}
}
public class TestFruit {
public static void main(String[] args) {
Person p = new Person(); // person object
Fruit f = new Fruit(); // fruit object
p.eat(f);
} // eat method of person class
}
person is eating grapes
人员类别:
public class Fruit {
String fruitname = "grapes";
}
public class Person {
void eat(Fruit f) {
System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
}
}
public class TestFruit {
public static void main(String[] args) {
Person p = new Person(); // person object
Fruit f = new Fruit(); // fruit object
p.eat(f);
} // eat method of person class
}
person is eating grapes
测试类:
public class Fruit {
String fruitname = "grapes";
}
public class Person {
void eat(Fruit f) {
System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
}
}
public class TestFruit {
public static void main(String[] args) {
Person p = new Person(); // person object
Fruit f = new Fruit(); // fruit object
p.eat(f);
} // eat method of person class
}
person is eating grapes
输出:
public class Fruit {
String fruitname = "grapes";
}
public class Person {
void eat(Fruit f) {
System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
}
}
public class TestFruit {
public static void main(String[] args) {
Person p = new Person(); // person object
Fruit f = new Fruit(); // fruit object
p.eat(f);
} // eat method of person class
}
person is eating grapes
为了访问类的字段,将创建该类的对象
我的问题是:
public class Fruit {
String fruitname = "grapes";
}
public class Person {
void eat(Fruit f) {
System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
}
}
public class TestFruit {
public static void main(String[] args) {
Person p = new Person(); // person object
Fruit f = new Fruit(); // fruit object
p.eat(f);
} // eat method of person class
}
person is eating grapes
在Person
类中,我如何访问fruitname
类的fruitname
字段(即编写f.fruitname
),而不在Person
类中实例化fruitname
类
fruitname
是Fruit
类的数据成员,在创建对象之前,实例成员不存在
我刚刚开始学习Java,我被困在这里了。请帮助我理解。您所做的工作不起作用,因为您没有将成员字段声明为
public
:
public String fruitname = "grapes";
只有这样,您才能编译以下内容:
System.out.println("person is eating " + f.fruitname);
注意,Java中的字段是packageprivate
per default()。这意味着该字段可以是private
,但在这种情况下,您只能在驻留在同一包中的类中访问该字段
但是,通常创建的getter和setter方法如下:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
这将允许您访问类成员fruitname
,如下所示:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
根据您的IDE,您可以右键单击字段(或类中的某个位置),并找到类似于
Generate..>getter和setter使整个行为不那么烦人。您所做的不起作用,因为您没有将成员字段声明为public
:
public String fruitname = "grapes";
只有这样,您才能编译以下内容:
System.out.println("person is eating " + f.fruitname);
注意,Java中的字段是packageprivate
per default()。这意味着该字段可以是private
,但在这种情况下,您只能在驻留在同一包中的类中访问该字段
但是,通常创建的getter和setter方法如下:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
这将允许您访问类成员fruitname
,如下所示:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
根据您的IDE,您可以右键单击字段(或类中的某个位置),并找到类似于Generate..>getter&setter
这使得整个行为不那么烦人。所以看起来你需要仔细阅读。这不是一件坏事!当你是初学者时,OO设计是很难的
要回答您的问题,您必须实例化水果名对象,然后将其标记为public(或者最好编写一个getter/setter)
使用以下内容创建此对象:
Fruit f=new Fruit("peach");
System.out.println(f.getName());
所以看起来你需要仔细阅读。这不是一件坏事!当你是初学者时,OO设计是很难的
要回答您的问题,您必须实例化水果名对象,然后将其标记为public(或者最好编写一个getter/setter)
使用以下内容创建此对象:
Fruit f=new Fruit("peach");
System.out.println(f.getName());
您的问题是,您没有正确封装水果类
当前字段是package private
,因此只有类本身和来自同一个包的其他类可以访问该字段。当开始使用并发时,您确实需要正确地封装字段以保护它们
我建议研究注释预处理器,因为它将在以后生成方法,从而对您有很大帮助。您只需要在类或其中应封装的字段上方添加两个注释
您的水果类的封装和文档化版本如下所示:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
您的问题是,您没有正确封装水果类
当前字段是package private
,因此只有类本身和来自同一个包的其他类可以访问该字段。当开始使用并发时,您确实需要正确地封装字段以保护它们
我建议研究注释预处理器,因为它将在以后生成方法,从而对您有很大帮助。您只需要在类或其中应封装的字段上方添加两个注释
您的水果类的封装和文档化版本如下所示:
public class Fruit {
private String fruitname = "grapes";
public String getFruitname() {
return fruitname;
}
public void setFruitname(String fruitname) {
this.fruitname = fruitname;
}
}
public class Person {
public void eat(Fruit f) {
System.out.println("person is eating " + f.getFruitname());
}
}
package me.yourname.yourproject;
import javax.annotation.Nullable;
public class Fruit {
@Nullable
private String name;
/**
* Constructs a fruit without a name.
*/
public Fruit(){
}
/**
* Constructs a fruit with an initial name.
*
* @param name The fruits initial name.
*/
public Fruit(String name){
this.name = name;
}
/**
* Sets the name of the fruit.
*
* @param name The fruits new name.
*/
public void setName(@Nullable String name){
this.name = name;
}
/**
* Gets the fruits current name.
*/
@Nullable
public String getName(){
return this.name;
}
}
如果您想亲自访问它,而不需要水果实例:
您的水果名是一个实例变量。通过将其声明为“静态”,可以使其成为类成员,然后可以使用Fruit.fruitname访问它
您可以将其设置为“公共”以允许从任何地方访问。如
public static string fruitname = "grapes";
现在,您不需要水果实例来访问水果名。
您的个人电话如下所示:
public class Person {
void eat() {
System.out.println("person is eating " + Fruit.fruitname);
}
}
如果您想亲自访问它,而不需要水果实例:
您的水果名是一个实例变量。通过将其声明为“静态”,可以使其成为类成员,然后可以使用Fruit.fruitname访问它
您可以将其设置为“公共”以允许从任何地方访问。如
public static string fruitname = "grapes";
现在,您不需要水果实例来访问水果名。
您的个人电话如下所示:
public class Person {
void eat() {
System.out.println("person is eating " + Fruit.fruitname);
}
}
您可以将类声明为静态的,比如:-静态类Fruit{String fruitname=“grapes”}我仍然远离Java中的“静态”概念。现在只练习创建简单对象。尝试给Fruit一个访问器(getter)方法,以返回名称。这是标准的Java语言,我不明白你的问题。您的代码已经实现了您想要的功能:水果的fuitname是通过Person.eat()方法访问的,Person类不会实例化任何水果。它从TestFruit主方法接收它作为参数。首先,您不是在Person
中实例化Fruit
——而是在主方法的外部进行实例化。您要做的是将Fruit
实例f
传递给Person
的eat()
函数,该函数需要这样一个实例。Insiceeat()