对初始化,实例化和界面的困惑

时间:2016-03-09 04:58:19

标签: java interface initialization

我是Java新手,正在尝试学习初始化的概念。我在网上看到了这句话:

List<Integer> numList  = Arrays.asList(1,2,3,4,5,6);

我理解接口类型无法实例化。但是Arrays.asList()方法返回一个实现List<>接口的对象。这句话是否算作初始化?我是否创建了一个实现List<>接口的类的实例?

3 个答案:

答案 0 :(得分:2)

是的,该声明计为初始化;您通过为numList变量赋值来初始化Arrays.asList()变量:Arrays.asList(arr)返回的值。

调用ArrayList时,它会根据输入数组arr实例化ArrayListAbstractList是抽象类List的特定实现,它本身实现ArrayList。因此,虽然List没有直接实现<form> <button class="button" formaction="myextension://" id = "start_app">Open My App</button> </form> 接口,但可以通过 polymorphism 对其进行处理。

有关详细信息,请参阅Java Tutorial on Creating ObjectsJava Tutorials on Interfaces,尤其是using an interface as a type部分。您还可以阅读有关InheritanceInitializing Fields的教程。

答案 1 :(得分:1)

参考变量可以声明为类类型接口类型

如果变量被声明为接口类型,它可以引用任何实现接口的类的任何对象。

所以你创建了一个被接口变量引用的对象。

答案 2 :(得分:0)

  • 此语句是否算作初始化?

    是的。因为您初始化了引用变量package ru.javafiddle.core.ejb; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.ws.rs.client.Entity; import ru.javafiddle.jpa.entity.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * * @author mac */ @Stateless public class UserBean { private static final int DEFAULT_USER_STATUS = 1; @PersistenceContext(name = "JFPersistenceUnit") EntityManager em; public UserBean(){} public User register(String firstName, String lastName, String nickname, String email, String passwordHash) { //search for registered class Status st = em.find(Status.class,DEFAULT_USER_STATUS); User user = new User(); user.setFirstName(firstName); user.setLastName(lastName); user.setNickName(nickname); user.setEmail(email); user.setPasswordHash(passwordHash); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); user.setRegistrationDate(dateFormat.format(date)); //em.getTransaction().begin(); em.persist(user); // em.flush(); // em.getTransaction().commit(); User uBase = getUser(nickname); return uBase; } public User getUser(String nickName) { User user; try { user = (User)em.createQuery("SELECT u FROM User u WHERE u.nickName =:nickname") .setParameter("nickname", nickName) .getSingleResult(); } catch (NoResultException noResult) { return null; } return user; } public User setUser(String nickName, String firstName, String lastName, String newNickName, String email, String passwordHash) { User user; try { user = (User)em.createQuery("SELECT p FROM User p WHERE p.nickName = :nickName") .setParameter("nickName", nickName) .getSingleResult(); } catch (NoResultException noresult) { return null; } // user = this.setFields(user, firstName, lastName, newNickName, email, passwordHash); return user; } //we need to delete all entries from usergroup table too for this user public User deleteUser(String nickName) { User user; user = (User)em.createQuery("SELECT p FROM User p WHERE p.nickName =:nickName") .setParameter("nickName", nickName) .getSingleResult(); UserGroup u = (UserGroup)em.createQuery("SELECT u from UserGroup u WHERE u.userId=:userid") .setParameter("userid", user.getUserId()) .getSingleResult(); /* for(Iterator<UserGroup> i = u.iterator(); i.hasNext(); ) { UserGroup u1 = i.next(); em.getTransaction().begin(); em.remove(u1); em.getTransaction().commit(); }*/ em.getTransaction().begin(); em.remove(user); em.getTransaction().commit(); return user; } public List<String> getUserProjects(String nickName) { // List<String> hashes = new LinkedList<String>(); // User user = (User)em.createQuery("SELECT u FROM User u WHERE u.nickName=:nickname") // .setParameter("nickname", nickName) // .getSingleResult(); // List<Group> groups = user.getGroups(); // // for (Group g:groups) { // // List<Project> projects = group.getProjects(); // for (Project p:projects) { // hashes.add(p.getHash().getHash()); // } // } // // return hashes; return null; } public User setFields(User user, String firstName, String lastName, String newNickName, String email, String passwordHash) { if(!firstName.equals("")) user.setFirstName(firstName); if(!lastName.equals("")) user.setLastName(lastName); if(!newNickName.equals("")) user.setNickName(newNickName); if(!email.equals("")) user.setEmail(email); if(!passwordHash.equals("")) user.setPasswordHash(passwordHash); em.getTransaction().begin(); em.persist(user); em.getTransaction().commit(); return user; } }

  • 我是否创建了一个实现List&lt;&gt;的类实例接口

    不,您没有创建实现List&lt;&gt;的类实例。 interface.But numList方法创建了一个类Arrays.asList()的实例,将该对象的引用返回给您。

如果你阅读了源代码,你会发现:

ArrayList

现在public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } 这个语句创建一个实例。 是的new ArrayList<>(a);实现ArrayList接口。