从数组传递o对象中的参数

时间:2018-04-20 11:14:06

标签: java arrays

我的问题是,我有一个日志对象的数组,参数是在同一个类中进行的。但问题是我不知道如何将名为logX logY等的参数转换为实际对象。

这是为了让对象出现在屏幕上,但是如果我能把它变成一个对象拳头那就可以解决这么多问题!

这是Log类:

package Frogger;

import javax.swing.JFrame;

public class Logs extends JFrame
{
    private int X;
    private int Y;
    private int width;
    private int height;


Logs[] LogArray = new Logs[10];
{

LogArray[0].X = 0;
LogArray[0].Y = 0;  
LogArray[0].width = 50; 
LogArray[0].height = 50;
}
}

这是主要的:

   package Frogger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;


public class Frogger_Main extends JFrame implements ActionListener
{
    private Container cPane;
    private JMenuBar mb;
    private JMenu mSystem;
    private JMenuItem mIRules, mIExit;
    private JButton btnExit, btnStart,btnReset;
    private JLabel lblTitle;
    private JPanel pNorth, pSouth, pCentre, pEast;
    private JPanel imagePanel;

    public void actionPerformed(ActionEvent e) 
    {/*For the main public class*/}



    public Frogger_Main()//Constructor
    {
        cPane = getContentPane();

        imagePanel = new JPanel()
        {
            public void paint(Graphics g)//The background thing
            {
                try
                {
                    BufferedImage image  = ImageIO.read(new File("Background.jpg"));
                    g.drawImage(image, 1, 1, null);
                } 
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        };
        imagePanel.setPreferredSize(new Dimension(500,500));

        cPane.add(imagePanel); // End of the background thing 
    }


}

这是将运行它的类:

package Frogger;

import javax.swing.JFrame;



public class Test_Frogger
{

    public static void main(String[] args) 
    {

        Frogger_Main f = new Frogger_Main();
        f.setTitle("Frogger");
        f.setSize(1280,980);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

2 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,你的public class Logs extends JFrame { private int X; private int Y; private int width; private int height; //strange and should not be there, but ok Logs[] LogArray = new Logs[10]; //totally nonsense piece of code { LogArray[0].X = 0; LogArray[0].Y = 0; LogArray[0].width = 50; LogArray[0].height = 50; } } 课程很奇怪......

package Frogger;

import javax.swing.JFrame;

public class Logs extends JFrame {
 private int x; //convention camelCase, start with small
 private int y;
 private int width;
 private int height;

  Logs(){
  //some kind of default constructor
    this.x = 0;   //int default value is zero, so you dont need to do this in fact
    this.y = 0;
    this.width = 50;
    this.height = 50;
  }

  //parametrized constructor
  Logs(int x, int y, int width, int height){
  //save values to class fields/ object properties
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }


  //because class fields are private, this is called setter
  public void setX(int x){
    this.x =x;
  }

  //because fields are private, this is called getter  
  public int getX(){
    return this.x;
  }

}

所以,你可以做类似

的事情
//create array of 10 logs, but each will be null
Logs[] logArray = new Logs[10];

//for each will be used non parametrized constructor- that "some kind of default"" 
for(int i = 0,i < logArray.length; i++){
  logArray[i] = new Logs(); 
}

然后你可以创建一个Log array

public Logs[] getLogs(int count){
  Logs[] logArray = new Logs[count];

  for(int i = 0,i < logArray.length; i++){
    logArray[i] = new Logs(); 
  }  
}

例如获取具有给定长度

的此数组的方法
logArray[0].setX(4); //for setting
logArray[0].getX(); //for read value, getting

请注意,因为有一个二传手,所以你可以制作例如。

    public static ILogger ConfigureLogging(string appName, string appVersion)
    {
        AppDomain.CurrentDomain.ProcessExit += (sender, args) => Log.CloseAndFlush();

        var configPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");
        var environmentName = configPackage.GetSetting("appSettings", "Inspired.TradingPlatform:EnvironmentName");

        var loggerConfiguration = new LoggerConfiguration()
             .WriteTo.File(@"D:\SvcFab\applog-" + appName + ".txt", shared: true, rollingInterval: RollingInterval.Day)
             .Enrich.WithProperty("AppName", appName)
             .Enrich.WithProperty("AppVersion", appVersion)
             .Enrich.WithProperty("EnvName", environmentName);

        var log = loggerConfiguration.CreateLogger();

        log.Information("Starting {AppName} v{AppVersion} application", appName, appVersion);

        return Log.Logger = log;
    }

答案 1 :(得分:0)

在Logs类中使用contructor

public class Logs extends JFrame {

    private int x,y,width,height;

    public Logs(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }


}

然后调用数组

Logs[] LogsArray = new Logs[10];

LogsArray[0] = new Logs(1,2,3,4);
LogsArray[1] = new Logs(3,2,3,5);