在放弃丑陋的bash脚本之后,我一直在学习如何在今天的大部分时间里使用Python.
我正在尝试使用2个类来定义一些对象数组,其中存储一些唯一的字符串和整数(1-10).对象将包括以下内容:
object[i].user
.n # n = i
.name
.coords
.hero
Run Code Online (Sandbox Code Playgroud)
(param1,param2,param3)对于每个object.n和object.user都是不同的,所以我在尝试使用一个在编写90个唯一字符串后看起来不像垃圾的赋值方法.嵌套我发现的例子不起作用,所以这里是妥协:
class CityBean:
def __init__(self,name,coords,hero):
self.name = name
self.coords = coords
self.hero = hero
class Castles:
def __init__(self,user,n):
self.user = user
self.n = n
if self.user == 'user1':
temp = {
1: CityBean( "name1" , "coord1" , "hero1"),
... blah blah blah
10: CityBean( "name10" , "coord10" , "hero10" )}[self.n]()
if self.user == 'user2':
temp = {
1: CityBean( "name11" , "coord11" , "hero11" ),
... blah blah blah …Run Code Online (Sandbox Code Playgroud) 嗯,我遇到了一个奇怪的问题。当我创建类的新实例时,我得到StackOverflowExcepion :)
这是代码:
public partial class PlayerChooser : Window
{
public PlayerChooser()
{
InitializeComponent();
textBoxPlayer1Name.Visibility = Visibility.Hidden;
textBoxPlayer2Name.Visibility = Visibility.Hidden;
textBoxPlayer3Name.Visibility = Visibility.Hidden;
textBoxPlayer4Name.Visibility = Visibility.Hidden;
}
public static String player1Name;
public static String player2Name;
public static String player3Name;
public static String player4Name;
...
PlayerChooser.player1Name = textBoxPlayer1Name.Text;
PlayerChooser.player2Name = textBoxPlayer2Name.Text;
TwoPlayers501_new twoPlayers501_new = new TwoPlayers501_new();
twoPlayers501_new.Show();
...
}
Run Code Online (Sandbox Code Playgroud)
以及发生异常的类和构造函数
public partial class TwoPlayers501_new : Window
{
public TwoPlayers501_new()
{
InitializeComponent();
textBlockPlayer1Name.Text = PlayerChooser.player1Name;
textBlockPlayer2Name.Text = PlayerChooser.player2Name;
}
...
}
Run Code Online (Sandbox Code Playgroud)
预先感谢,这可能有点琐碎...
A级1
public class A {
private static final A instance = new A();
public static A getInstance() {
return new A();
}
}
Run Code Online (Sandbox Code Playgroud)
A级2
public class A {
private static final A instance = new A();
private A(){}
public static A getInstance() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
我刚开始学习单例,我看到了两个使用A 1示例和A 2示例的Java示例.A 1级getInstance()是单身吗?我也想知道这两个A类getInstance()方法有什么区别?谢谢
我正在尝试学习OOP,从Java开始,我已经阅读并被告知它是最好的起点.话虽如此,我正在尝试创建一个有趣的游戏来帮助我学习,但我也知道游戏编程和设计可能更具挑战性.
因此,我的目标是从RollDice D20生成新值,而无需重置或重新启动程序.您会注意到,当我打印出值时,我会打印同一个实例两次,以演示我要避免的内容,以及一个新实例,以显示新实例确实生成了一个新值.也许,我并没有以正确的方式接近这一点,但这是一个障碍,我希望在一些帮助下克服!
我最终想要的是弄清楚如何根据需要多次生成新实例或至少一个新的滚动值.非常感谢任何和所有的帮助!我在下面添加了代码作为示例.此外,任何其他反馈表示赞赏.
import java.util.Random;
class RollDice
{// Begin RollDice Class
// Initiate method rollDice
public static int rollDice(int number, int nSides)
{
// System.out.println( "--- Welcome to the Dice Game v2! ---" ); //
// welcomes player
Random r = new Random();
// Declare class variables
int num = 0;
int roll = 0;
if (nSides >= 3)
{
for (int i = 0; i < number; i++)
{
roll = r.nextInt(nSides) + 1;
// System.out.println("Roll is: " …Run Code Online (Sandbox Code Playgroud) 这是我尝试创建的类:
package rectangle;
public class Rectangle
{
private double length,width;
public void setLength(double length)
{
length=this.length;
}
public void setWidth(double width)
{
width=this.width;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double area()
{
return length*width;
}
}
Run Code Online (Sandbox Code Playgroud)
我相信我已经正确地上课了.我只是想创建并使用一个可以计算矩形区域的类.
然后我尝试实际创建实例对象:
/*Testing out the rectangle class*/
package rectangleclasstest;
import java.util.Scanner;
public class RectangleClassTest
{
static void main(String[] args)
{
Scanner keyboard= new Scanner(System.in);
Rectangle rec=new Rectangle();
//get length
System.out.println("Please enter the length");
rec.setLength()=keyboard.nextInt(); …Run Code Online (Sandbox Code Playgroud) 我需要在Crop of Plant类中创建一个实例,我无法找到一种方法来使其工作.希望你们能帮忙.
这是我的主要地方,我将一个对象发送到类作物.
int main(){
char select = 'a';
int plant_num = 2;
int crop_num = 0;
Crop crop[10];
Plant plant[20];
plant[0] = Plant("Carrots",'c');
plant[1] = Plant("Lettuce",'l');
plant[2] = Plant("Rosemary",'r');
crop[0] = Crop(plant[0],4,2);
crop_num++;
cout << "Garden Designer" << endl;
cout << "===============";
}
Run Code Online (Sandbox Code Playgroud)
class crop是我想要植物类实例的地方
class Crop {
private:
int g_length;
int w_width;
Plant PlantType;
public:
Crop();
Crop(const Plant&, int, int);
};
Run Code Online (Sandbox Code Playgroud)
我想要类作物实例的类植物
class Plant {
private:
char plant[20];
char p_symbol;
public:
Plant();
Plant(const char* n, char s); …Run Code Online (Sandbox Code Playgroud) 我试图找到我的问题的解决方案.我的问题是我得到一个名为的参数值"key",其数据类型是Object.任何一种使用如此的在开始之前key我想首先检查是不是Integer还是String和任何形式的data-type(主要目的是检查之间Integer和String).
我已经尝试了谷歌,但我没有任何用处.
请提供一些解决方案.
许多人提前感谢.
例
if(key is Integer){
//do something here
}else{
// do something here
}
Run Code Online (Sandbox Code Playgroud) 我有一个类Node如下:
public class Node
{
public Dictionary<string, string> dictionary;
public Node(Dictionary<string, string> dictionary)
{
this.dictionary = dictionary;
}
public void CreateNode()
{
this.dictionary.Add("1", "String1");
Dictionary<string, string> dictionary1 = new Dictionary<string, string>();
Console.WriteLine(this.dictionary["1"]);
Node tmp = new Node(dictionary1);
tmp.dictionary = this.dictionary;
Console.WriteLine(tmp.dictionary["1"]);
tmp.AddE(tmp, "String2","2");
Console.WriteLine(this.dictionary["2"]);
}
public void AddE(Node tmp,String text,string c)
{
tmp.dictionary.Add(c,text);
}
}
Run Code Online (Sandbox Code Playgroud)
Node有一个包含字符串键和值的字典,一个带参数的构造函数,一个CreateNode()方法,它将一个项添加到字典中并创建另一个Node.现在,在tmp.dictionary = this.dictionary之后; 在tmp.dictionary上添加了另一个项目,但是它也添加在this.dictionary中(我不希望发生这种情况,我很想丢失).
主要方法:
static void Main(string[] args)
{
Dictionary<string, string> dictionary …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,我正在编写我的第一个数据结构.
data Nat = Null | N Nat
Run Code Online (Sandbox Code Playgroud)
例如: 5是N (N (N (N (N Null))))
我必须从中创建实例 Show, Eq, Ord, Num, Enum
我Eq已经写过并且有效.
instance Eq Nat where
(==) Null Null = True
(==) (N Null) (N Null) = True
(==) Null (N Null) = False
(==) Null (N xs) = False
(==) (N xs) (N xs2) = xs == xs2
Run Code Online (Sandbox Code Playgroud)
但是当我在拥抱中尝试这个时它会给我一个错误("错误 - 控制堆栈溢出").
我不能继续下去.
(N (N Null)) :: Nat
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
基本上,我想创建一个对象,然后将该对象添加到一堆不同的数组中,并确保如果一个数组更改了对象的值,则其他数组中的其他对象不会更改其值。
例如,假设我宣布一把枪,其中50枚子弹以整数形式存储:
Gun tommygun = new Gun(50);
Run Code Online (Sandbox Code Playgroud)
我有两名士兵,每个士兵都有枪支清单,并在每个士兵中都添加了一个“地雷枪”。
Soldier1.Guns.Add(tommygun);
Soldier2.Guns.Add(tommygun);
Run Code Online (Sandbox Code Playgroud)
士兵1开枪:
Soldier1.Shoot(Soldier1.Guns[0]);
Run Code Online (Sandbox Code Playgroud)
这会使Soldier1的弹药减少1。现在是49。这是否还会减少Soldier2的弹药?如果是这样,我如何避免在不为每位士兵创建单独的弹药的情况下避免这种情况?