interface Father{
String name="zhangjun;
void introduce();
"}
public class demo1{
public static void main(String[] args){
class Child implements Father{
public void introduce() {
System.out.println("父亲的名字叫")+this.name;
}
}
selfIntroduce(new Child());//1
//2请在此处添加代码使用匿名内部类实现注释1的功能
//3请在此处添加代码使用Lambda表达式实现注释1的功能}
public static void selfIntroduce(Father f){
f.introduce;
}
}
import java.util.function.Consumer;
/**
* 父亲接口
*/
interface Father{
//在接口中定义变量必须要初始化
String name="zhangjun";
/**
* 介绍行为,说明父亲具有介绍自己的行为
*/
void introduce();
}
public class demo1{
public static void main(String[] args) {
/**
* 孩子类,实现了父亲接口
*/
class Child implements Father {
/**
* 对于介绍行为的实现,就是输出自己的名字
*/
public void introduce() {
System.out.println("父亲的名字叫"+ this.name);
}
}
//对于自我介绍的调用
selfIntroduce(new Child());//1
//2请在此处添加代码使用匿名内部类实现注释1的功能
//因为Father是一个接口,无法创建实例,创建的这个实例对应的对象没有名字,和Child不一样,因为没有类名,所以是匿名类
new Father() {
@Override
public void introduce() {
System.out.println("父亲的名字叫"+ this.name);
}
}.introduce();
//3请在此处添加代码使用Lambda表达式实现注释1的功能
Consumer<Father> lambda = (father)->System.out.println("父亲的名字叫"+ father.name);
lambda.accept(new Child());
}
/**
* 一个自我介绍行为,出入一个实现了Father接口的类
* @param f 实现father接口的实例
*/
public static void selfIntroduce(Father f){
f.introduce();
}
}
interface Father{
String name="zhangjun";
void introduce();
}
class demo1 {
public static void main(String[] args) {
class Child implements Father {
@Override
public void introduce() {
System.out.println("父亲的名字叫"+ name);
}
}
selfIntroduce(new Child());
selfIntroduce(() -> {
System.out.println("父亲的名字叫"+ Father.name);
});
}
public static void selfIntroduce(Father f){
f.introduce();
}
}
尽力了
/**
* 接口中定义的常量是静态常量(编译时将编译为公有静态常量)
*/
public interface Father {
public static String name = "zhangjun";
void introduce();
}
class Demo {
public static void main(String[] args) {
class Child implements Father {
@Override
public void introduce() {
System.out.println("父亲的名字叫"+ name);
}
}
selfIntroduce(new Child());
//匿名类
Father anonymousObj = new Father() {
@Override
public void introduce() {
System.out.println("父亲的名字叫"+ Father.name);
}
};
selfIntroduce(anonymousObj);
//lambda
selfIntroduce(() -> {
System.out.println("父亲的名字叫"+ Father.name);
});
}
public static void selfIntroduce(Father f){
f.introduce();
}
}
如有帮助,请采纳!!!!!!