我有一堂有两种方法的课程。
在 中method1(),我创建了一个local record名为Abc. 这local record仅适用于method1()因为它是在(这里是根据 Java 的规则)中定义的。method1()
在 中method2(),我创建了一个local interface名为ABC. 请注意,这里存在大小写差异。该local record被命名了Abc,但这个local interface被命名了ABC。
这是班级。
//package ..... //commenting out package information, as I sincerely doubt that is the cause
/** There seems to be a class loader error when running the below method in main(). */
public class ClassLoaderIssue
{
/** Method …Run Code Online (Sandbox Code Playgroud) 我只是在试验内部类,并且遇到了这个有本地但是静态内部类的想法......我在静态方法中创建了一个内部类......好吧,它就像那样简单..这是我做的例子
class Outer {
static void m() {
class LocalStatic {
void s() {
System.out.println("static local inner class method");
}
}
}
}
class Demo {
public static void main(String args[]) {
Outer.m();
}
}
Run Code Online (Sandbox Code Playgroud)
这不会给出任何编译错误.
我知道如何访问静态方法m.但我想知道是否有办法 从外部类访问本地类LocalStatic ..那么根据我的理解,我们无法访问方法内的某些内容吗?因此,我不能访问任何LocalStatic或任何方法或类之外的本地类的内部属性外只是想确保..
我有以下 C++ 片段代码。在main()函数内部声明一个类。
我们不能在本地类中定义友元函数是什么原因?
#include<iostream>
int main()
{
class Foo
{
void foo() {} // Ok
friend void Bar(){}; // Error
};
}
Run Code Online (Sandbox Code Playgroud) 请看一下这个片段:
public class A {
void method() {
System.out.print(B.j);//This is legal!
class C {
void method () {
System.out.print(j);//This is illegal!
}
}
final int j = 10;
class D {
void method() {
System.out.print(j);//This is legal!
}
}
}
}
class B {
static int j = 10;
}
Run Code Online (Sandbox Code Playgroud)
我们可以在定义之前访问某个地方的'Bj',而在C类访问'final int j'的情况下这是非法的.
java编译器将本地类看作简单的变量/对象吗?特别是,这种行为背后的理由是什么?我的意思是前向检查正在为Bj工作,但它不适用于C类中的'j'.
如果我有这个代码.
public class Test{
{
class People {
}
}
public static void main(String[] args) {
People person = new People();//Compile ERROR
}
}
Run Code Online (Sandbox Code Playgroud)
我无法创建People的实例.
这是否意味着Initializer Block不能用于定义类?
public class Main {
public static void main(String[] args) {
int b=1;
final int c=2;
String s1[] = new String[]{"A","B","C"};
class InnerMain{
int a=5;
public void show(){
System.out.println(s1[0]);
System.out.println("A:" + a);
System.out.println("B:" + b);
System.out.println("C:" + c);
}
}
InnerMain test =new InnerMain();
test.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我研究过的这本书说,本地类只能使用本地类所在的final变量和方法的引用.在这个例子中,我使用的b是不是final或引用的变量.它跑了,我没有得到任何错误.怎么样?有人可以解释这种行为吗?
请考虑以下两个类:
一个学生
package datatypes;
public class Student {
private String name ;
public Student(String name) {
this.name = name;
}
class Address{
String city;
String state;
Address(String city , String state){
this.city = city;
this.state = state;
}
String getAddress(){
return city + state;
}
}
String getName(){
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
b.)StudentDemo
package datatypes;
import datatypes.Student.Address;
public class StudentDemo {
public static void main(String[] args) {
Student obj = new Student("Yati");
Address adr = obj.new Address("YNR" , "HARYANA");
System.out.println(obj.getName()); …Run Code Online (Sandbox Code Playgroud) 在我测试时,下面的代码执行没有任何问题.但我无法理解逻辑.有人可以解释一下吗?
public static void main(String[] args) {
List<String> london = new ArrayList<>(Arrays.asList("Chinatown","Croydon","Eden Park"));
class City{
private String name;
City(String name){
this.name = name;
}
public void printCity(){
System.out.println(name);
}
}
london.stream().map(City::new).forEach(City::printCity); //Chinatown,Croydon,Eden Park
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例代码中,我有以下问题.
local-class ×8
java ×7
c++ ×1
c++11 ×1
class ×1
classloader ×1
field ×1
friend ×1
identifier ×1
java-stream ×1
lambda ×1