这是同一个问题的略微详细版本.
我们无法访问子类中受保护的变量(超类),其中子类位于不同的包中.我们只能访问supeclass的继承变量.但是如果我们将修饰符更改为'protected static',那么我们也可以访问超类的变量.为什么会那样.?
这是我试图解释的代码片段.
package firstOne;
public class First {
**protected** int a=7;
}
package secondOne;
import firstOne.*;
public class Second extends First {
protected int a=10; // Here i am overriding the protected instance variable
public static void main (String [] args){
Second SecondObj = new Second();
SecondObj.testit();
}
public void testit(){
System.out.println("value of A in Second class is " + a);
First b = new First();
System.out.println("value in the First class" + b.a ); // Here compiler throws …Run Code Online (Sandbox Code Playgroud) 我面临一个棘手的情况,我必须在具有嵌套列表的对象上使用 groupingBy。我用 map()、flatmap()、toMap() 尝试了一些东西,但无法想出一个解决方案,只是在兜圈子。非常感谢来自流专家的任何帮助。我需要在我的 OrdersAnalyzer 类中实现 2 个方法。这是我的对象的样子:
public class Order {
private final String id;
private final Set<OrderLine> orderLines = new HashSet<>();
private final Customer customer;
//getters, setters, equals, hashcode omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
public class OrderLine {
private final Product product;
private final Integer quantity;
//getters, setters, equals, hashcode omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
public class Product {
private final String name;
private final BigDecimal price;
//getters, setters, equals, hashcode omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
public class OrdersAnalyzer {
/** …Run Code Online (Sandbox Code Playgroud) 我有一个名为Employee的类,它具有employeeName和employeeId作为其成员变量.我正在创建新的Employee对象,然后将其添加到TreeSet我想要根据它对其进行排序的位置employeeId.但我认为2个Employee对象如果相同则相等employeeName.设置不允许重复.但在这里我可以观察到一种奇怪的行为.这是我的代码.(我不是在这里使用getter和setter.我直接访问成员变量.)
package secondOne;
import java.util.Set;
import java.util.TreeSet;
class Employee implements Comparable<Employee> {
String employeeName;
int employeeId;
public Employee(String name, int id) {
this.employeeName = name;
this.employeeId = id;
}
public int compareTo(Employee emp) {
//return this.employeeName.compareTo(emp.employeeName);
return (this.employeeId - emp.employeeId);
}
@Override
public String toString() {
return ("Name is: " + employeeName + " Emp id is: " + employeeId);
}
@Override
public boolean equals(Object emp) {
if (emp instanceof …Run Code Online (Sandbox Code Playgroud)