简体   繁体   English

如何使用与 Java 中的这个等效的算法从 Python 中的 class 创建多个对象?

[英]How do you create multiple objects from a class in Python using an equivalent algorithm to this one in Java?

I've written a piece of code in Java to create multiple objects for a problem solving exercise.我在 Java 中编写了一段代码来创建多个对象来解决问题。 This worked successfully, but I'm now trying to write it in Python and can't work out how to do it.这很成功,但我现在正试图用 Python 编写它,但不知道该怎么做。 I want to have a list of objects, each with unique values, that I can use in the rest of my code.我想要一个对象列表,每个对象都有唯一的值,我可以在我的代码的 rest 中使用它。

Here's my Java code:这是我的 Java 代码:

public void createSquares() {
        int square = 0;
        int count = 0;
        for (int r=1; r<10; r++) {
            for (int c=1; c<10; c++) {
                if (r<4 && c<4) {
                    square = 1;
                }
                else if (r<4 && c>3 && c<7){
                    square = 2;
                }
                else if (r<4 && c>6){
                    square = 3;
                }
                else if (r>3 && r<7 && c<4){
                    square = 4;
                }
                else if (r>3 && r<7 && c>3 && c<7){
                    square = 5;
                }
                else if (r>3 && r<7 && c>6){
                    square = 6;
                }
                else if (r>6 && c<4){
                    square = 7;
                }
                else if (r>6 && c>3 && c<7){
                    square = 8;
                }
                else if (r>6 && c>6){
                    square = 9;
                }
                Square s = new Square(r, c, square, count);  //How does this work in Python?
                sList.add(count, s);
                count++;
            }
        }
    }

In python, constructors (in a method called __init__ ) accept a parameter called self representing the object itself, before any other parameter.在 python 中,构造函数(在名为__init__的方法中)在任何其他参数之前接受一个名为self的参数,该参数表示 object 本身。

class Square:
    def __init__(self, r, c, square, count): # Create the constructor, accepting 4 parameters
        self.r = r
        self.c = c
        self.square = square
        self.count = count

And in your main loop:在你的主循环中:

s = Square(r, c, square, count) # Notice there is no "self" here, and the keyword "new" is not used
list.append(s) # Python lists use "append" and not "add"
count += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用不同的类对象创建一个类的数组? 爪哇 - how to create an array of one class using a different classes objects? Java 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java? 如何在Java中创建多个对象? - How do I create multiple objects in Java? 如何在一行中读取和处理可以包含多个对象的行 - How do you read and process a line that can have multiple objects on one line JAVA 如何使用Java通过对象名称打印类的属性? - How do you print a class's attributes by the objects name using java? Java-如何将对象作为参数传递给该对象类的构造函数 - Java - How do you pass an object as an argument into a constructor from that objects class 如何从文本文件创建对象数组列表,并使每个 object 成为 java 中的数组? - How do you create an array list of objects from a text file and have each object be an array in java? 如何创建一些对象来存储在一个arrayList中,这是一个子类的属性,来自Java中的父类? - How do I create some objects to store in an arrayList, which is a attribute of sub class, from a parent class in Java? Java-创建多个对象后如何避免StackOverflow? - Java - How do you avoid a StackOverflow after creating multiple objects? Java:如何按顺序返回多个对象? - Java: How do you return multiple objects in order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM