Java 类,该类统计创建的对象数+;随机数发生器

Java 类,该类统计创建的对象数+;随机数发生器,java,Java,好的,在这里完成noob,学习如何编码。先说声对不起 我的任务是创建一个类“Ping”,其中包含一个类varieble,用于跟踪我创建了多少个“Ping对象”。这些Ping对象应有4个属性,其值在创建对象时随机生成。这些值应为0-255之间的整数。创建这些对象时,这些属性也应按如下格式打印: 172.15.256.1 192.165.0.0 10.100.68.215 到目前为止,我唯一的代码就是这个,我很确定它完全是无用的lol。非常感谢您的帮助 package ping; import j

好的,在这里完成noob,学习如何编码。先说声对不起

我的任务是创建一个类“Ping”,其中包含一个类varieble,用于跟踪我创建了多少个“Ping对象”。这些Ping对象应有4个属性,其值在创建对象时随机生成。这些值应为0-255之间的整数。创建这些对象时,这些属性也应按如下格式打印:

172.15.256.1 192.165.0.0 10.100.68.215

到目前为止,我唯一的代码就是这个,我很确定它完全是无用的lol。非常感谢您的帮助

package ping;

import java.util.Random;

public class Ping {
    public static void main(String[] args) {
        Random rnd = new Random();


        System.out.println();
        for (int i = 0; i < 4; i++) {
            int r1 = rnd.nextInt(255);
            int r2 = rnd.nextInt(255);
            int r3 = rnd.nextInt(255);
            int r4 = rnd.nextInt(255);



          System.out.println(r1 + "." + r2 + "." +r3 + "." + r4 );```
```OUTPUT 

141.143.64.132

20.121.58.51

40.54.128.90

226.161.89.26```
包ping;
导入java.util.Random;
公开课{
公共静态void main(字符串[]args){
随机rnd=新随机();
System.out.println();
对于(int i=0;i<4;i++){
int r1=rnd.nextInt(255);
int r2=rnd.nextInt(255);
int r3=rnd.nextInt(255);
int r4=rnd.nextInt(255);
系统输出打印LN(r1+“+r2+”+r3+“+r4)```
```输出
141.143.64.132
20.121.58.51
40.54.128.90
226.161.89.26```

好的,您需要为
Ping
类定义字段和构造函数(如果需要检索值,请考虑添加getter方法)。为了找出您创建了多少Ping,您需要使用静态字段(无论从何处访问静态字段,静态字段都具有相同的值,并且可以使用类进行访问,如
Ping.number
,您不一定需要
Ping
对象的实例)object,调用构造函数,并随机初始化所有4个字段。此外,
number
是递增的,因此这就是您实际跟踪创建的对象数量的方式。请注意,
toString
方法用于将
Ping
对象转换为字符串,以便可以很好地打印到控制台

public class Ping {
    int r1;
    int r2;
    int r3;
    int r4;
    
    public static int number = 0;
    
    Ping () {
        number++;
        Random rnd = new Random();
        r1 = rnd.nextInt(255);
        r2 = rnd.nextInt(255);
        r3 = rnd.nextInt(255);
        r4 = rnd.nextInt(255);
    }
    
    @Override
    public String toString() {
        return r1 + "." + r2 + "." + r3 + "." + r4;
    }
    
    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println("pings: " + Ping.number + "\np1: " + ping1 + "\np2: " + ping2);
    }
}
输出:

pings: 2
p1: 177.127.106.216
p2: 164.221.52.251
Ping Count: 2
Ping1: 212.55.226.115
Ping2: 140.132.194.210

使用新的
Java8
数组
库,您可以用更少的代码行为您的类获得更高性能的解决方案。 您还需要重写
toString()
方法才能打印ping对象

import java.util.Random;
import java.util.stream.*;
import java.util.Arrays;

class Ping {
    private static int count = 0;
    private int[] arr = new int[4]; 
   // Declare them as an array instead of separate variables.

    Ping () {
        ++count;
        Random rnd = new Random();
        Arrays.setAll(arr, i -> rnd.nextInt(255)); // similar to for(int i = ...)
    }

    public static int getCount() {
        return count;
    }
    // The stream collects all ints into a '.' separated string.
    @Override 
    public String toString() {
        return Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining("."));
    }

    public static void main(String[] args) {
        Ping ping1 = new Ping();
        Ping ping2 = new Ping();
        System.out.println("Ping Count: " + Ping.getCount() + "\nPing1: " + ping1 + "\nPing2: " + ping2);
    }
}
输出:

pings: 2
p1: 177.127.106.216
p2: 164.221.52.251
Ping Count: 2
Ping1: 212.55.226.115
Ping2: 140.132.194.210

你应该从头开始:1.我的任务是创建一个类“Ping”@Nilsbergstroom看看我的答案,看看它是否适合你。