如何制作一种更新类属性的方法? [英] How to make a method to update attributes of a class?

查看:89
本文介绍了如何制作一种更新类属性的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用以下类图来编写Java代码.

在我的任务描述中,陈述了以下内容; 类图,用于更新属性的方法完全缺少一件事.这是因为系统的客户端已决定不允许对其进行更新.但是,我们知道这一要求至少会随着年龄的变化而改变.因此,任务还包括设计和实现一种更新年龄的方法.这样做时,请记住年龄只会增加,而永远不会减少."

因此,我的问题是我该如何制作一种方法来更新类的属性-在这种情况下会增加年龄? :)

下面还有2个其他要求(所以您知道我为什么要在代码中包括它)... 狗的尾巴长度可以用整齐的公式计算: 尾巴长度=ålder⋅vikt10svanslängd=ålder⋅vikt10 该公式适用于除出租车以外的所有犬只.一税总尾长为3.7.由于狗舍的值是国际性的,因此系统必须正确处理瑞典语"tax"和英语"dachshund"."

这是我的代码:

public class Dog {
    private String name;
    private String breed;
    private int age;
    private int weight;

    public String getName() {
        return name;
    }

    public String getBreed() {
        return breed;
    }

    public int getAge() {
        return age;
    }

    public int getWeight() {
        return weight;
    }

    public double getTailLength() {
        double length;
        // Undantag för taxar/dachshunds (alltid svanslängd 3,7). 
        if (breed.equalsIgnoreCase ("Tax")  || breed.equalsIgnoreCase("Dachshund")) {
            length = 3.7;
        } else {
            //Formel för svanslängd
            length = (double)(age * weight) / 10;
        }

        return length;
    }

    public String toString() {
        return String;
    }
}

解决方案

只需保持getter和setter不变-在这方面遵循Java bean规范. 您尝试做的事情不仅仅是设置值-例如,对于setter for age属性.在这种情况下,最好有另一种方法,该方法根据您要插入的逻辑来增加使用期限.选择新方法将一如既往地要求拥有适当的Javadoc. >

I'm supposed to make a code in Java out of the following class diagram.

In the description of my assignment the following is stated; "One thing is completely missing from the class diagram, methods for updating the attributes. This is because the client of the system has decided that it is not allowed to update them. However, we know that this requirement will change, at least for age. The task therefore also includes designing and implementing a method for updating the age. When you do so, keep in mind that age can only increase, never decrease."

My question is therefore How do I make a method to update attributes of a class - in this case increase age? :)

2 other requirements down below (so you know why I included it in my code)... "The tail length of a dog can be calculated with the neat formula: tail length = ålder⋅vikt10svanslängd = ålder⋅vikt10 This formula applies to all dogs except taxis. One tax always has the tail length 3.7. Since kennel values ​​are international, both the Swedish word "tax" and the English "dachshund" must be handled correctly by the system."

Here is my code:

public class Dog {
    private String name;
    private String breed;
    private int age;
    private int weight;

    public String getName() {
        return name;
    }

    public String getBreed() {
        return breed;
    }

    public int getAge() {
        return age;
    }

    public int getWeight() {
        return weight;
    }

    public double getTailLength() {
        double length;
        // Undantag för taxar/dachshunds (alltid svanslängd 3,7). 
        if (breed.equalsIgnoreCase ("Tax")  || breed.equalsIgnoreCase("Dachshund")) {
            length = 3.7;
        } else {
            //Formel för svanslängd
            length = (double)(age * weight) / 10;
        }

        return length;
    }

    public String toString() {
        return String;
    }
}

解决方案

Just keep the getters and setters as is - follow the java bean spec in this regard. You are trying to do more than setting the value - for eg in the case of setter for age attribute. In this case, it will be better to have another method ,which increments the age according to the logic that you want to put in. The choice to have a new method , will mandate the need to have proper Javadoc, as always.

这篇关于如何制作一种更新类属性的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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