用java编写一个圆类_如何用Java编写只写类?

用java编写一个圆类

The question is that "can we make a write-only class in Java?"

问题是“我们可以用Java编写只写类吗?”

The answer is: "Yes, we can make write-only in Java."

答案是: “是的,我们可以使用Java进行只写操作。”

在Java中定义只写类 (Defining a write-only class in Java)

Now, we will see in few steps, how to make write-only class and the various steps in given below,

现在,我们将在几个步骤中看到如何制作只写类以及下面给出的各个步骤,

We can make a class write-only by making all of the data members private.

我们可以通过将所有数据成员设为私有来使类仅写。

Please note:

请注意:

  • If we made a class write-only then we can modify the properties or data member value of the class.

    如果我们将类设为只写,则可以修改该类的属性或数据成员值。

  • If we made a class write-only then we can only write the properties or data members value of the class.

    如果我们将类设为只写,则只能写入该类的属性或数据成员值。

  • The write-only class will contain setter methods which write the value of the private properties because there is a setter method available in the class.

    只写类将包含用于写入私有属性值的setter方法,因为该类中提供了setter方法。

  • The write-only class can contain getter method if we want to read the value of the private properties after writing.

    如果我们要在写入后读取私有属性的值,则只写类可以包含getter方法。

Now, we will see the objective of the setter method, why it is required?

现在,我们将看到setter方法的目标,为什么需要它?

Few points need to remember about setter methods are given below:

以下是关于设置方法的一些要点:

  • As we know that "private" data member of the class is accessible in the same class only.

    众所周知,该类的“私有”数据成员只能在同一类中访问。

  • Let suppose we want to access "private" data member of the class in outside class. So, in that case, we need to declare public "setter" methods.

    假设我们要在外部类中访问该类的“私有”数据成员。 因此,在这种情况下,我们需要声明公共的“ setter”方法。

  • The objective of the set method is used to update or set the private variable values.

    set方法的目标用于更新或设置私有变量值。

Syntax:

句法:

    public void setDataMember_Name(Type var_name);

In the setter method, it is not mandatory the same data member name after set, but it is convenient for our understanding that we should consider the same name as the data member after set.

在setter方法中,设置后并不一定要具有相同的数据成员名称,但是对于我们理解而言,方便的是,我们应该考虑与设置后的数据成员具有相同的名称。

There are few advantages of Setter Methods are given below:

设置方法的优点如下:

  • Hiding the internal representation of the private data member.

    隐藏私有数据成员的内部表示。

  • Setter methods provide access level hierarchy.

    设置方法提供访问级别层次结构。

  • This method adds additional functionality easily later.

    此方法以后可以轻松添加其他功能。

  • This class allows getter methods to be protected properties from unexpected changes by outside class.

    此类允许getter方法受到保护,以防止外部类意外更改属性。

  • The private data member is updateable from outside the class with the help of setter methods.

    私有数据成员可以使用setter方法从类外部进行更新。

Example:

例:

// Java program to demonstrate the example of 
// making write-only class in Java

public class WriteWeeks {
    // Private Data Member Declaration
    private String days;

    // Defining Setter method to write the value of
    // private properties and this method takes an argument 
    // and assign it in the private member.
    public void setDays(String days) {
        this.days = days;
    }

    // Defining Getter method to retrive the value of 
    //private variable

    public String getDays() {
        return days;
    }

    public static void main(String[] args) {
        // Weeks object instanstiation
        WriteWeeks w = new WriteWeeks();

        // Set the value of the private member
        w.setDays("7 Days");

        String result = w.getDays();
        // Display the value of the private properties
        System.out.println("Days in a Week :" + " " + result);

    }
}

Output

输出量

Days in a Week : 7 Days


翻译自: https://www.includehelp.com/java/how-to-make-a-write-only-class-in-java.aspx

用java编写一个圆类

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值