如何在另一个类中引用类。

huangapple go评论75阅读模式
英文:

How to reference class in another class

问题

以下是翻译好的代码部分:

轮子类,接受两个输入(字符串、双精度浮点数):

public class Wheel
{
    private String name;
    private double radius;
    private double circumference = 2 * Math.PI * radius;

    /**
     * 轮子类的构造方法
     */
    public Wheel(String name, double radius)
    {
       this.name = name;
       this.radius = radius;
    }
    /**
     * 得到周长变量的访问方法
     */
    public double getCircumference()
    {
        return circumference;
    }
    
}

引擎类中引用轮子类:

public class Engine
{
    // 实例变量 - 用你自己的示例替换下面的内容
    private String[] name;
    private double tpl;
    private double totalNumTurns;
    private double distanceToTravel;
    Wheel wheel = new Wheel("Wichelin15", 15);

    public double getDistanceToTravel()
    {
        this.distanceToTravel = wheel.getCircumference() * tpl;
        return distanceToTravel;
    }
}

但我不想在这里输入参数,因为这两个类将在另一个类中被引用,那里会提供这两个类的参数。有什么帮助吗?

英文:

So I made a wheel class that takes in two inputs(String, double):

public class Wheel
{
    private String name;
    private double radius;
    private double circumference =  2 * Math.PI * radius;

    /**
     * Constructor for objects of class Wheel
     */
    public Wheel(String name, double radius)
    {
       this.name = name;
       this.radius = radius;
    }
    /**
     * Accessor method for Circumference variable
     */
    public double getCircumference()
    {
        return circumference;
    }
    
}

and I want to referece it in my engine class as so:

public class Engine
{
    // instance variables - replace the example below with your own
    private String[] name;
    private double tpl;
    private double totalNumTurns;
    private double distanceToTravel;
    Wheel wheel = new Wheel("Wichelin15", 15);

public double getDistanceToTravel()
    {
        this.distanceToTravel = wheel.getCircumference() * tpl;
        return distanceToTravel;
    }
}

but I dont want to have to put in the arguments as these two classes are going to be referenced in another class where the arguments for both will be put in.
Any help?

答案1

得分: 0

你需要为 Engine 创建一个构造函数,该构造函数接受 Wheel 作为参数。然后你可以在管理对象初始化的类中使用这个构造函数。例如:

class VehicleManager {
  Vehicle createVehicle(... 可以在这里放置参数我不知道是什么参数...) {
    Wheel wheel = new Wheel(...);
    Engine engine = new Engine(wheel, ...);
    return new Vehicle(engine, ... 其他零件和属性 ...);
  }
}

这样你就不需要在 Engine 类中硬编码一个 Wheel 对象。

同时,看起来你在引擎的名称上使用了 String[] 而不仅仅是一个 String,这是有意为之吗?

英文:

You need to create a constructor for Engine that accepts Wheel as an argument. Then you can use this constructor in the class that manages your objects' initialisation. For example:

class VehicleManager {
  Vehicle createVehicle(... you could put arguments here idk ...) {
    Wheel wheel = new Wheel(...);
    Engine engine = new Engine(wheel, ...);
    return new Vehicle(engine, ... other parts and attributes ...);
  }
}

That way you don't have to hardcode a Wheel object in your Engine class.

It also looks like you're using a String[] for the name of the engine instead of just a String, is that intentional?

huangapple
  • 本文由 发表于 2020年10月17日 23:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64403907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定