如果你看一下像cocos2d-x这样的框架,我会尽力说清楚
cc.Sprite.extend();
Run Code Online (Sandbox Code Playgroud)
这里的Sprite是类,我们可以调用方法而不用new关键字实例化它
我有一个班级
var superClass = function(){
this.init = function(){
console.log("new class constructed !")
};
};
Run Code Online (Sandbox Code Playgroud)
要调用init我必须这样做
obj = new superClass();
obj.init();
Run Code Online (Sandbox Code Playgroud)
但是如何在不实例化的情况下引用类的方法
我正在使用Phaser框架,我想创建一个新的类,在phaser中保存sprite类的所有属性,所以我尝试这样做
var mario = function(){
Phaser.Sprite.call(this); // inherit from sprite
};
Run Code Online (Sandbox Code Playgroud)
但是有一个错误"Uncaught TypeError:undefined is not a function"
然后我试过了
var mario = function(){
this.anything = "";
};
mario.prototype = new Phaser.Sprite();
Run Code Online (Sandbox Code Playgroud)
确定它有效,但它调用了相位器构造函数,我不想创建精灵,直到我这样做 var heroObj = new mario();
我该怎么办 ?
即时学习摇摆我写了一个代码来显示jpanel中的简单文本区域,但只有面板显示不是文本区域.
主框架类
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame{
private TextPanel textPanel;
private JButton button;
public MainFrame(){
super("Hello World!");
this.textPanel = new TextPanel();
this.button = new JButton("Click me");
this.setLayout(new BorderLayout());
this.add(textPanel,BorderLayout.CENTER);
this.add(button,BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 500);
this.setVisible(true);
}
private void add(TextPanel textPanel2, String center) {
// TODO Auto-generated method stub
};
}
Run Code Online (Sandbox Code Playgroud)
以及包含面板和文本区域的第二个类
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; …Run Code Online (Sandbox Code Playgroud)