【问题标题】:Java 8 Innner Class InstantiationJava 8 内部类实例化
【发布时间】:2017-09-04 14:06:28
【问题描述】:

为什么这段代码无法编译。我正在学习用 Java 进行谓词。这是代码

package com.java8.lambda.predicate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.java8.lambda.pojo.Employee;

public class PredicateSample {
    public static void main(String[] args) {
        Employee permanentEmployee = new Employee(70, "Jagmohan", "Sanyal", new Date(), 'M', 19900.87,"P");
        Employee tempEmployee = new Employee(70, "Shruti", "Bhatia", new Date(), 'F', 1990.87,"C");

        List<Employee> empList = new ArrayList<>();
        empList.add(permanentEmployee);
        empList.add(tempEmployee);

        PredicateSample predicateSample = new PredicateSample();
        //Code doesn't compile on this line due to non-visibility of EmployeePredicate class
        //It fails with "PredicateSample.EmployeePredicate cannot be resolved to a type"
        EmployeePredicate empPredicate = new PredicateSample().new EmployeePredicate();
    }

}

class EmployeePredicate {

    public Predicate<Employee> getOlderEmployee(){
        return p -> p.getAge() > 60  && p.getEmploymentType().equalsIgnoreCase("P");
    }

    public List<Employee> filter(List<Employee> employees, Predicate<Employee> p){
        return employees.stream().filter(p).collect(Collectors.toList());
    }
}

我已经通过以下链接Inner Class Instantiation

我也浏览了其他网站,但无法找出问题所在。

【问题讨论】:

  • EmployeePredicate 不是PredicateSample 的内部类

标签: java java-8 inner-classes


【解决方案1】:

您需要内部类真正成为一个内部类。

public class PredicateSample {
    public static void main(String[] args) {
        Employee permanentEmployee = new Employee(70, "Jagmohan", "Sanyal", new Date(), 'M', 19900.87,"P");
        Employee tempEmployee = new Employee(70, "Shruti", "Bhatia", new Date(), 'F', 1990.87,"C");

        List<Employee> empList = new ArrayList<>();
        empList.add(permanentEmployee);
        empList.add(tempEmployee);

        PredicateSample predicateSample = new PredicateSample();
        //Code doesn't compile on this line due to non-visibility of EmployeePredicate class
        //It fails with "PredicateSample.EmployeePredicate cannot be resolved to a type"
        EmployeePredicate empPredicate = new PredicateSample().new EmployeePredicate();
    }

    // **inner** class.
    class EmployeePredicate {

        public Predicate<Employee> getOlderEmployee(){
            return p -> p.getAge() > 60  && p.getEmploymentType().equalsIgnoreCase("P");
        }

        public List<Employee> filter(List<Employee> employees, Predicate<Employee> p){
            return employees.stream().filter(p).collect(Collectors.toList());
        }
    } // < --- end of **inner** class
} // < --- end of **outer** class.

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2012-11-05
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多