在java int,float等中,是原始类型.我们需要使用包装类来使用泛型.但是仍然以下声明在java中有效,
Class<Integer> intClass=int.class
Run Code Online (Sandbox Code Playgroud)
int.class即使它是原始类型,我们怎么称呼?
考虑我有以下枚举类,
public enum Sample {
READ,
WRITE
}
Run Code Online (Sandbox Code Playgroud)
在下面的课程中我试图测试枚举类,
public class SampleTest {
public static void main(String[] args) {
testEnumSample(Sample.READ);
}
public static void testEnumSample(Sample sample) {
System.out.println(sample);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我指定Sample.READ然后将它作为参数传递给方法testEnumSample.相反,如果我们想要实例化枚举类并将其作为参数传递给我们需要做什么?
请考虑以下代码段:
package vehicle;
public abstract class AbstractVehicle {
protected int speedFactor() {
return 5;
}
}
package car;
import vehicle.AbstractVehicle;
public class SedanCar extends AbstractVehicle {
public static void main(String[] args) {
SedanCar sedan = new SedanCar();
sedan
.speedFactor();
AbstractVehicle vehicle = new SedanCar();
// vehicle //WON'T compile
// .speedFactor();
}
}
Run Code Online (Sandbox Code Playgroud)
SedanCar是AbstractVehicle包含protected方法的子类speedFactor.speedFactor如果它被同一个类引用,我可以调用该方法.当超类用于参考时,该方法speedFactor不可访问.
隐藏方法的原因是什么?
考虑团队和球员两个班级.
Class Player
{
String name;
int age;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
代表球队的最佳方式是什么?
1)
Class Team
{
String teamName;
String city;
List<Player> players;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
2)
Class Team
{
String teamName;
String city;
//Getters and Setters
}
Class TeamPlayers
{
Team team;
List<Player> players;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
第一种形式对我来说似乎更合乎逻辑,但后者提供了更大的灵活性.那么我可以决定哪一个以及这两种方法的可能的利弊.
以下代码段
val keys=List(3,2,1,0)
val unsorted=List(1, 2, 3, 4)
val sorted =keys map unsorted
Run Code Online (Sandbox Code Playgroud)
基于的排序keys.
通常,map方法接受lambda并将其应用于源中的每个元素.相反,上面的代码采用一个列表并根据索引进行排序.
在这种特殊情况下发生了什么?
+ =(或任何赋值运算符)是用于Int类型的scala中的方法.
例如,
var x=5
x+=1
Run Code Online (Sandbox Code Playgroud)
这里我只能在变量时使用+ =方法.
我无法做到,
5+=1
Run Code Online (Sandbox Code Playgroud)
scala编译器是否将此方法视为特例?
为什么它在scala.Int类中不可用?
我在java中使用粒子群优化(PSO).我对我们的工作知之甚少.因为,我正在申请生物信息学中的多序列比对.
我们需要找到对齐这些序列的位置和速度.我需要有关PSO的详细解释和参考,以及计算PSO中速度和位置的需要.如果可能的话,我需要在java中解释PSO的简单示例.实际上,我需要了解它如何优化问题.
public class Position {
private double x;
private double y;
public Position(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
这是用getter和setter表示粒子位置的类
同样明智的其他课程在这里
当我尝试在构造函数中创建具有自引用的对象时,我得到StackOverflowError.
public class Example1 {
private int i;
private Example1 zero;
public Example1(int i) {
super();
if (i > 0) {
this.i = i;
} else {
this.i = this.zero.i;
}
this.zero = new Example1(i);
}
public int getI() {
return i;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用静态引用时,不会发生错误.
public class Example2 {
private int i;
private static final Example2 ZERO = new Example2(0);
public Example2() {
this(ZERO.i);
}
public Example2(int i) {
super();
this.i = i;
}
public int getI() {
return i;
} …Run Code Online (Sandbox Code Playgroud)