如果有两种不同的数据类型,但它们具有类似的功能:
type model = String
type priceOfC = Int
data Car = Cars model priceOfC
ComparePricesCar :: Car -> Car -> Int
.... (some codes here to compare prices between two cars)
type color = String
type priceOfB = Int
data Bike = Bikes color priceOfB
ComparePricesBike :: Bike -> Bike -> Int
.... (some codes here to compare prices between two bikes)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是实现类型类"流量",让Car和Bike成为Traffic的实例.并且所有实例都将实现一个名为"comparePrice"的函数.
我试过了:
class Traffic a where
comparePrice :: a -> a -> Int
instance Traffic Car where …Run Code Online (Sandbox Code Playgroud) 想想这个问题
function F(){}; //This is a Constructor function
Run Code Online (Sandbox Code Playgroud)
谁能告诉我两者之间有什么不同
var myInstance = new F;
Run Code Online (Sandbox Code Playgroud)
和
var myInstance = new F();
Run Code Online (Sandbox Code Playgroud)
?无论如何,新的关键字执行后立即执行函数?
javascript constructor instance new-operator javascript-objects
作为一个实验,我写了一些看起来像的代码
class MyClass
@var = 100
def hello
p "hello"
end
end
Run Code Online (Sandbox Code Playgroud)
我知道,如果我这样做MyClass.new是@var不是那个对象上定义的,而我认为这将定义@var的类MyClass.
这有实际用途吗?
是否有一个keyword引用当前类类型类似于如何this引用当前实例?
我的目的是避免在引用静态成员或枚举时输入完整的类名,但我想用"this"之类的东西来为它提供可读性,就像我为实例成员所做的那样.原因是一些类名非常庞大并导致过度包装.
我有这门课,我不明白到底是怎么回事:
public class Table {
private int[][] array;
private int N;
// constructor
public Table(int[][] array) {
N = array.length;
this.array = Arrays.copyOf(array, N);
}
// this method returns another Table object
public Table tweak() {
int[][] tweak = Arrays.copyOf(array, N);
// here I change the array
return new Table(tweak);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我调用tweak方法时,用于调用方法的对象也会发生变化:
public class TestCls {
public static void main(String[] args) {
int[][] array = {{1, 2},
{3, 4}};
Table a = new Table(array);
System.out.println(a.toString());
/* this will print …Run Code Online (Sandbox Code Playgroud) 我正在使用Weka Java API,我有一段代码.在代码中,我试图做如下的事情:
for (each instance i in the training/test set)
if (condition == TRUE)
remove instance (i) from training/test set;
Run Code Online (Sandbox Code Playgroud)
[编辑]例如,我有1000个实例,我试图查看每个实例,如果满足特定条件.如果条件为真,那么我将从训练/测试集中删除该实例.
我相信Weka没有以这种方式直接删除实例的选项.任何建议,专业人士?
坦率地说,我甚至不知道它是否可能.但我想要做的就是下面.
从这里开始,我只是粘贴我的代码.
公共类Main {
private static final String CLASS_FILE_PATH =
"/Users/juneyoungoh/Downloads/ClassFile.class";
private static final String JAVA_FILE_PATH =
"/Users/juneyoungoh/Downloads/ClassFile.java";
private static Class getClassFromFile(File classFile) throws Exception {
System.out.println("get class from file : [" + classFile.getCanonicalPath() + " ]");
Object primativeClz = new Object();
ObjectInputStream ois = null;
ois = new ObjectInputStream(new FileInputStream(classFile));
primativeClz = ois.readObject();
ois.close();
return primativeClz.getClass();
}
public static void main(String[] args) throws Exception {
getClassInfo(getClassFromFile(new File(CLASS_FILE_PATH)));
}
Run Code Online (Sandbox Code Playgroud)
}
就像你的假设一样,这段代码有错误.例如,它显示:
java.io.StreamCurruptedException: invalid …Run Code Online (Sandbox Code Playgroud) 我是Java的新手,当我尝试创建DeckofCards类的实例时,我的main函数出现错误.DeckofCards类应该是"卡片"对象的私有数组.我认为我的问题出现在DeckofCards类中,出于某种原因,我认为它不是一个数组?也许我创造了它错了?
我的主要错误是 '('或''''预期 和 数组需要,但DeckofCards发现
这是我的主要功能:
public static void main(String[] args) {
Card myCard = new Card(13,1);
System.out.println(myCard.getSuit());
System.out.println(myCard);
DeckofCards myDeck = new DeckofCards; //error here
for(int i=0; i<53; i++) {
System.out.println(myDeck[i]); //second error here
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的DeckofCards类:
public class DeckofCards {
private Card[] deck = new Card[52];
public DeckofCards(){
int i = 0;
for(int s = 1; s<5; s++){
for(int r = 1; r<14; r++){
deck[i].rank = r;
deck[i].suit = s;
i++;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我,我是否缺少一些语法或某些东西会很棒!谢谢.
所以我正在从我的课堂教材开始使用C++:早期对象的一个例子,我遇到了一个问题.我被指示创建一个程序,读取一些数据并在最后打印它,总共2个实例.当第二个实例(md2)运行时,它不允许我在第一个字段中输入任何内容.这真让我烦恼,我无法弄清楚.提前致谢!
#include <iostream>
using namespace std;
class MovieData
{
private:
string movieTitle;
string movieDirector;
int movieYear;
int movieRunTime;
public:
MovieData()
{
GetTitle();
GetDirector();
GetYear();
GetRunTime();
}
void GetTitle()
{
cout << "Specify A Title\n";
getline(cin, movieTitle);
cout << endl;
}
void GetDirector()
{
cout << "Specify A Director" << endl;
getline(cin, movieDirector);
cout << endl;
}
void GetYear()
{
cout << "Specify A Year" << endl;
cin >> movieYear;
cout << endl;
}
void GetRunTime()
{
cout << …Run Code Online (Sandbox Code Playgroud) instance ×10
java ×4
class ×3
.class-file ×1
arrays ×1
c# ×1
c++ ×1
constructor ×1
haskell ×1
heroku ×1
javascript ×1
keyword ×1
new-operator ×1
newrelic ×1
object ×1
python ×1
reflection ×1
ruby ×1
this ×1
variables ×1
weka ×1