退货&打印字段值还是仅通过类方法打印值? [英] Return & print field value or just print value through class method?

查看:102
本文介绍了退货&打印字段值还是仅通过类方法打印值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Java,并且正在学习封装,我不确定以下哪个是更好的做法:

  1. 使用吸气剂将字段值从一个类返回到另一个类,然后通过另一个类中的方法将其打印出来.
  2. 从另一个类中调用一个类中的方法以打印该字段的值.

该值不被操作,仅通过System.out.println();显示;

任何建议将不胜感激:)

一个类(人)保存有关人的信息,例如姓名,年龄,体重等.另一类(人)具有一种用于搜索人的LinkedList的方法,以查找具有匹配年龄的对象的方法.如果找到一个人,则该方法将打印出该人的姓名.

解决方案

封装就是维护 ,而所有逻辑都包含在在该类的私有方法中封装.如果其他类在做自己的小事情的过程中需要从第一类中了解事情,那么您可以在该类中提供获取方法,以公开这些东西,而无需详细说明它们的方式在内部实施.

特别是对于您的问题,您提到的选项实际上是同一件事:getter 一种被其他类调用以返回字段值的方法.这种方法的优点是,它在包含它的类中将该字段封装,然后可以解析/重新计算/存储该字段,或者以其他方式与该字段进行交互,只要它为返回期望的数据类型.

为了说明,假设您创建一个具有double balance字段的BankAccount类.您进行了一些测试,它似乎运行良好,因此您创建了引用此balance字段的其他几个类.在某些时候,您会注意到您的一些计算下降了几美分.您进行了一些研究,发现使用double存储货币值是一种不好的做法,而应该使用名为BigDecimal的类.

如果您的其他类直接访问了balance字段,则它们都必须进行更改(即使它们从未直接使用过,也都必须导入BigDecimal),以便于进行此更改.另一方面,如果他们通过getter方法访问帐户的balance,则可以在BankAccount中将balance的类型更改为BigDecimal. 在将getBalance()的返回类型保留为double的同时,通过调用BigDecimaltoDouble()方法以返回其他类期望的double值:您其他的类都不会知道您已经更改了数据类型.

表示关注点分离的另一种方法是说每个类都应该有一个改变的单一理由(这是encapsulation and I'm unsure which of the following is a better practice:

  1. Use a getter to return a field value from one class to another and then print it through a method in another class.
  2. Call a method in a class from another class to print the value of the field.

The value isn't manipulated, only shown through System.out.println();

Any advice would be appreciated :)

EDIT: One class (Person) holds information about people, such as name, age, weight etc. and another class (Directory) has a method used to search through a LinkedList of people to find a object with a matching age. If a person is found, then the method prints out the name of the person.

解决方案

Encapsulation is all about maintaining a separation of concerns, the core idea of which is that one class should know as little as possible about how other classes work, partly so that you can make changes to those classes without having to change other classes that interact with them.

Broadly: As a rule of thumb, you want each of your classes to do "it's own little thing" - to have its own little concern, with all the logic that goes into doing that little thing encapsulated in private methods of that class. If other classes, in the course of doing their own little things, need to know things from the first class, then you provide getter methods in that class that expose those things without exposing the details of how they are implemented internally.

With respect to your question specifically, the options you mention are actually the same thing: A getter is a method that gets called by other classes to return the value of a field. The advantage of such a method is that it encapsulates that field in the class that contains it, which can then parse/recalculate/store or otherwise interact with that field however it pleases, as long as it's getter returns the expected data type.

To illustrate, imagine you create a BankAccount class with a double balance field. You do a few tests and it seems to work fine, so you create several other classes that reference this balance field. At some point, you notice that some of your calculations are coming up a few cents off. You do some research and find out that it's a bad practice to use double to store monetary values, and that you should be using a class called BigDecimal instead.

If your other classes accessed your balance field directly, they would all have to change (and all would have to import BigDecimal even though they never use it directly) in order to facilitate this change. On the other hand, if they access an account's balance by way of a getter method, you can change the type of balance to BigDecimal in BankAccount while leaving the return type of getBalance() as double, by calling BigDecimal's toDouble() method to return the double value your other classes expect: None of your other classes will ever know you've changed the data type.

Another way of stating the idea of separation of concerns is to say that each class should have a single reason to change (this is the single responsibility principle referenced in @GregKopff's comment): Needing to change the data type of the balance field is a valid reason for BankAccount to change, but would it be a valid reason or all the other classes that interact with it to change? Should you have to change your BankAccountHolder or BankEmployee class because a technical detail in the Account class changed?

This might not seem to answer your question directly, but I think the only answer to this question in general is to illustrate the question you should ask yourself to answer it each time you come across it... which will happen just about every time you write a class.

If my illustration is unclear, please let me know how I can clarify it: You've asked an important question, and it's important that you grasp the answer to it (as well as what you're asking.)

这篇关于退货&打印字段值还是仅通过类方法打印值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆