使用子类对象修改其超类对象中的受保护属性

发布于 2024-08-30 15:58:02 字数 1136 浏览 9 评论 0原文

抱歉这个蹩脚的标题我没有为我的 Java 问题想出更好的版本。 我现在使用 Java 版本:1.6.0_18 和 Netbeans 版本:6.8

现在回答问题。

我所做的是创建一个只有一个受保护的 int 属性的类,接下来我创建了一个公共方法来将 int 属性设置为给定值。 然后我创建了该类的一个对象,并使用所述公共方法将 int 属性设置为 5。 现在我需要您的帮助来创建另一个类,该类将采用所述对象并公开它的 protected int 属性。

我想到的方法是创建一个子类来继承该类,然后创建一个方法来获取超类的 int 属性。我成功地创建了获取 int 属性的代码,但现在我不知道如何使用这个新的子类来引用超类的对象。

以下是我迄今为止拥有的 2 个类:

public class A
{
  protected int iNumber;

  public void setNumber ( int aNumber )
  {
    iNumber = aNumber;
  }
}
public class B extends A
{
  public int getNumber()
  {
   return super.iNumber;
  }
}

我创建了一个“A”对象,并使用其方法将其属性设置为 5,如下所示:

A objA = new A();
objA.setNumber ( 5 );

现在我想创建一个“B”对象来输出存储在“objA”的属性。 我尝试运行此代码:

B objB = (B) objA;
String aNumber_String = String.valueOf( objB.getNumber() );
System.out.println( aNumber_String );

但我收到错误:第一行出现“java.lang.ClassCastException”

 B objB = (B) objA; 

请问有办法做我想做的事吗?

PS 我希望让这个想法发挥作用,因为我不想通过给它一个 getter 方法来编辑 A 类(除非我别无选择)。

PPS 我也知道公开属性而不是使其私有并使用公共 setter / getter 方法是一个“坏”主意,但我喜欢这种方式:)。

编辑:添加代码标签

Sorry for the crappy title I failed to think of a better version for my Java question.
I am right now using Java version: 1.6.0_18 and Netbeans version: 6.8

Now for the question.

What I've done is created a class with only one protected int property and next I made a public method to Set the int property to a given value.
Then I made an object of that class and used said public method to set the int property to 5.
Now I need your help to create another class that will take said object and expose it's protected int property.

The way I could think of doing this was to create a sub class to inherit said class and then create a method to Get the int property of the super class. I kind of succeeded to create the code to Get the int property but now I can't figure out how to use this new sub class to reference the object of the super class.

Here are the 2 classes I have thus far:

public class A
{
  protected int iNumber;

  public void setNumber ( int aNumber )
  {
    iNumber = aNumber;
  }
}
public class B extends A
{
  public int getNumber()
  {
   return super.iNumber;
  }
}

I created an object of 'A' and used its method to set its property to 5, like this:

A objA = new A();
objA.setNumber ( 5 );

Now I want to create an object of 'B' to output the int stored within the property of 'objA'.
I've tried to run this code:

B objB = (B) objA;
String aNumber_String = String.valueOf( objB.getNumber() );
System.out.println( aNumber_String );

but I got the error: "java.lang.ClassCastException" on the first line

 B objB = (B) objA; 

Please is there anyway of doing what I am trying to do?

P.S. I am hoping to make this idea work because I do not want to edit class A (unless I have no choice) by giving it a getter method.

P.P.S Also I know it's a 'bad' idea to expose the property instead of making it private and use public setter / getter methods but I like it this way :).

Edit: Added code tags

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-09-06 15:58:02

对于该行,

B objB = (B) objA;

A 类的对象不是 B 类的对象,因此不允许进行强制转换。

AB 之间的类关系是,B is-a A ,(因为类 B 扩展了 A),但在这种情况下不能说相反的情况。

以以下为例,其中存在以下内容:

  • Animal 类。
  • class Dog extends Animal

上面的示例中尝试的是将 Animal 转换为 Dog

Dog dogObject = (Dog)animalObject;  // Not allowed.

:情况不一定如此,因为并非所有 Animal 都是 Dog ——据我们所知,animalObject 可能是Cat 类扩展了 Animal,它绝对不是 Dog

For the line

B objB = (B) objA;

the object of class A is not a object of class B, so that cast would not be allowed.

The class relationship between A and B is, that B is-a A, (because class B extends A), but the inverse cannot be said in this case.

Take the following for example, where the following exists:

  • class Animal.
  • class Dog extends Animal

What is being attempted in the above example is to cast an Animal to a Dog:

Dog dogObject = (Dog)animalObject;  // Not allowed.

This cannot be necessarily the case, as not all Animals are Dogs -- for all we know, the animalObject could be an instance of the class Cat which extends Animal, which is definitely not a Dog.

不一样的天空 2024-09-06 15:58:02

您无法将 A 对象强制转换为 B。如果要调用 getNumber,则必须有一个真正的 B 对象:

B objB = new B();
objB.setNumber ( 5 );
System.out.println( objB.getNumber() );

不需要强制转换。

You can't cast a A object to B. If you want to call getNumber, you must have a real B object:

B objB = new B();
objB.setNumber ( 5 );
System.out.println( objB.getNumber() );

No casts are necessary.

看海 2024-09-06 15:58:02

正如其他人所说,由于 A 对象与 B 之间的关系,您不能将它们强制转换为 B 对象。我要补充的是,听起来确实好像您只有一个类,但在某些情况下您希望其他对象无法读取数字的值。我想说这最好通过界面来实现。将接口定义为仅具有 setNumber(int) 方法。定义该类以同时具有 getNumber() 和 setNumber(int) 方法,并实现“setter”接口。然后,无论您有一个接受 A 对象作为参数的方法,请将其更改为接受您的接口。

这将允许您拥有不允许获取对象值的代码,但通过仅定义一个类来简化您的代码。

As others have said, you can't cast an A object to a B because of their relationship. What I'd add is that it really sounds like you only have one class, but you have some situations in which you want some other objects not to be able to read the value of the number. I'd say this is better served with an interface. Define the interface to only have the setNumber(int) method. Define the class to have both the getNumber() and the setNumber(int) methods, and to implement your "setter" interface. Then, wherever you have a method that accepts an A object as a parameter, change it to accept your interface.

This will allow you to have code that is not allowed to get the value of the object, but simplifies your code by only having one class defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文