在Java中使用'='运算符设置自定义类对象的值

编程入门 行业动态 更新时间:2024-10-27 19:25:40
本文介绍了在Java中使用'='运算符设置自定义类对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个自定义对象,它有一个int类型的值,我想要进行处理以将此值保持在设定范围内。我的问题是:给定下面的类,我可以用myObject = 0设置它的值;

I have a custom object that has a single value of type int that I wanting to do processing on to keep this value in a set range. My question is this: Given the following class, can I set it's value with myObject = 0;

public class foo{ private int bar; public foo(){ } }

而不是创建方法public void setBar()

Instead of creating a method public void setBar()

推荐答案

如果您的意思是:

foo x = new foo(); x = 10; // This is meant to set x.bar

然后不,你不能用Java做到这一点。好的,如果你问我......在可读性方面会很糟糕。

then no, you can't do that in Java. Good thing too, if you ask me... it would be horrible in terms of readability.

你也无法改变它允许:

foo x = 10;

相当于:

foo x = new foo(); x.bar = 10; // Or x.setBar(10);

更多推荐

在Java中使用'='运算符设置自定义类对象的值

本文发布于:2023-10-29 03:05:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   运算符   对象   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!