我写了以下代码
class Hello //Note the class is not public
{
public static void main(String args[]) {
System.out.println("Hello");
}
}
因此,当我运行它时,它运行正常并打印输出"Hello".
但是,如果JVM规范要求主要方法应该是公开的,因为"它不能看到主要的",那么它不应该也适用于类吗?如果JVM"未能看到"Hello.main(),当它未被声明为public时,它如何能够看到A类本身.
除了"因为规范是这样说的"之外,还有其他解释吗?
如果JVM能够看到所有类和方法,因为它本身就是"安全/可见性执行器",那么为什么需要将main方法声明为public.
我得到了一个练习,我需要使用类型别名来"引用3个整数".虽然我发现成功使用typedef我无法通过using引入来复制它c++11.
代码:
typedef int (& int_ref)[3]; \\success
using int_ref2 = (int &) [3]; \\error
我应该只使用类似......
using int_ref2 = int [3];
int_ref2 & iruvar ...
我很抱歉,如果这是重复但我没有找到任何相关的东西.
那么,如何使用以下代码打印0具有整个平方根的数字?
for (n = 1.0; n <= 10; n++)
{
Console.WriteLine ("Fractional Part : {0 :#.####}", (Math.Sqrt(n) - (int) Math.Sqrt(n)));
}
Run Code Online (Sandbox Code Playgroud)
假设我对以下两种调用默认构造函数(由编译器提供)创建对象的方法的了解是正确的。
class A
{
int a,b;
//No programmer defined constructor
}
...
A o1; //Implicit Call I believe
A o2 = A(); //Explicit Call
Run Code Online (Sandbox Code Playgroud)
为什么会A o2 = A();导致对象的成员(a,b)o2使用默认值(0)初始化自身,而不是A o;(它们使用垃圾值初始化)?这是未定义的行为吗?
在旧的Turbo C++编译器上编程C时,我可以使用"conio.h"头文件的clrscr()方法,但不能使用Dev C++ 5.4.2.(它给出了一个不寻常的错误ID返回1退出状态.虽然它有与clrscr()无关,当我删除clrscr()语句时,它完全正常!)因此,方法clrscr()方法已被弃用.这个错误是什么意思?
还有一个问题是"是与语言相关的编译器和库".因此对于特定的编译器,相应的库就像绑定它一样.
为什么我不允许使用以下语法在类的构造函数体中调用成员对象/不同类的构造函数?
class Circle {
double radius;
public:
Circle(double r) : radius(r) { }
double area() {return radius*radius*3.14159265;}
};
class Cylinder {
Circle base;
double height;
public:
Cylinder(double r, double h) {
base(r);
height = h;
}
double volume() {return base.area() * height;}
};
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我知道我可以Circle::Circle(double)通过Cylinder(double,double)使用成员初始化列表 调用,Cylinder(double r,double h) : base(r), height(r) {}但仍然有什么错误的编译器生成此错误的前一种方法?