java-接口

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41792311/article/details/79552991

    Java接口(interface)是一系列方法的声明,是一些方法特征的集合,接口可以说是完全抽象的抽象类,在接口里可以定义属性和方法,其中属性的默认修饰为public static final,方法的默认修饰为public abstract,而且这些方法只有特征没有实现,即不能实例化,下面有解释例子:

//定义用户类

package com.geminno.entity;

public class User {
private int uid;
private String uname;
private String password;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int uid, String uname, String password) {
super();
this.uid = uid;
this.uname = uname;
this.password = password;
}
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", password="
+ password + "]";
}

}

//定义接口,接口里的方法不用实现

package com.geminno.intf;
import com.geminno.entity.User;
public interface UserIntDao extends UserIntdao1{
void insert(User user);
User[] select(User user);
void delete(int id);

}

//定义另一个接口,测试接口的继承,继承此接口的接口还要实现此接口里的抽象类

package com.geminno.intf;
public interface UserIntdao1 {
void delete(int id);

}

//接口的实现

package com.geminno.intf;
import com.geminno.entity.User;
public class UserIntDaoImpl implements UserIntDao{//在接口的实现中重写接口类定义的未实现的方法
public void insert(User user) {
// TODO Auto-generated method stub
System.out.println("用户插入成功");
System.out.println(user);
}
public User[] select(User user) {
// TODO Auto-generated method stub
return null;
}
public void delete(int id) {
// TODO Auto-generated method stub
}

}

//测试接口的实现

package com.geminno.intf;
import com.geminno.entity.User;
public class TestUserIntDaoImpl {
public static void main(String[] args) {
UserIntDao u=new UserIntDaoImpl();//向上转型
User user=new User(001,"jack","123456");//设置用户
u.insert(user);//调用方法
}
}

展开阅读全文

没有更多推荐了,返回首页