class

Adding new objects to an ArrayList in constructor

旧街凉风 提交于 2020-11-28 02:54:27
问题 I am trying to add newly created objects to an ArrayList in the constructor of a class. The new objects are being created in another class in the main method. Main method: public static void main(String[] args) { // TODO code application logic here Player p1 = new Player("Peter"); } My Player class: public class Player { protected static int age; protected static String name; protected static ArrayList players = new ArrayList(); Player(String aName) { name = aName; age = 15; players.add(new

Adding new objects to an ArrayList in constructor

我的梦境 提交于 2020-11-28 02:54:08
问题 I am trying to add newly created objects to an ArrayList in the constructor of a class. The new objects are being created in another class in the main method. Main method: public static void main(String[] args) { // TODO code application logic here Player p1 = new Player("Peter"); } My Player class: public class Player { protected static int age; protected static String name; protected static ArrayList players = new ArrayList(); Player(String aName) { name = aName; age = 15; players.add(new

Adding new objects to an ArrayList in constructor

[亡魂溺海] 提交于 2020-11-28 02:52:19
问题 I am trying to add newly created objects to an ArrayList in the constructor of a class. The new objects are being created in another class in the main method. Main method: public static void main(String[] args) { // TODO code application logic here Player p1 = new Player("Peter"); } My Player class: public class Player { protected static int age; protected static String name; protected static ArrayList players = new ArrayList(); Player(String aName) { name = aName; age = 15; players.add(new

TypeScript setTimeout loop passing this error

百般思念 提交于 2020-11-27 04:57:11
问题 Trying to create a timer loop in TypeScript: timeout() { setTimeout(function () { console.log('Test'); this.timeout(); }, 1000/60); } But after the first loop works correctly I'm getting this error: "Uncaught TypeError: this.timeout is not a function". It seems that the this variable does not exist after the initial loop. Any ideas? 回答1: Because your this doesn't refer to the object. Every function has it's own this. So your this is the one which is defined by anonymous function inside the

TypeScript setTimeout loop passing this error

↘锁芯ラ 提交于 2020-11-27 04:55:44
问题 Trying to create a timer loop in TypeScript: timeout() { setTimeout(function () { console.log('Test'); this.timeout(); }, 1000/60); } But after the first loop works correctly I'm getting this error: "Uncaught TypeError: this.timeout is not a function". It seems that the this variable does not exist after the initial loop. Any ideas? 回答1: Because your this doesn't refer to the object. Every function has it's own this. So your this is the one which is defined by anonymous function inside the

用C写有面向对象特点的程序

廉价感情. 提交于 2020-11-20 07:06:05
转载自:陈皓 http://blog.csdn.net/haoel/archive/2003/04/02/2864.aspx 比如在一个项目中,有大量的数据结构,他们都是双向链表,但又想共用一套对链表的操作算法,这怎么做到呢,C中又没有C++中的继承,不然我可以继承一父(类中只有两个指针,一个向前一个向后),而其算法可以写在你类中的虚函数中,供子类使用。如: class Links { public: Links* back; Links* forword; virtual Add(){ ... }; virtual Del(){ ... }; virtual Ins(){ ... }; virtual Print() =0; .... }; 于是对于特定的数据结构我们可以: class mylinks : public Links { public: char* myname; char sex; int age; ... virtual Print(){ .... } }; 对其操作时都可以使用你类的泛型算法。 在C中,该如何做呢?我们用C中的指针和强制类型转可以做到。 下面是我总结出来的一个小的程序,体现了用指针的弹性来实现这一继承的效果:(我在Liniux下的GCC调试通过) ======================================= #include