我们调用对象都是通过类名来调用(带名字调用)
例如:
class shiyi{
public void show() {System.out.println("调用了show方法");}
}
public class test {
public static void main(String[] args) {//带名字的调用shiyi sy = new shiyi();sy.show();}
}
而匿名对象是这样的
例如
class shiyi{
public void show() {System.out.println("调用了show方法");}
}
public class test {
public static void main(String[] args) {//带名字的调用//shiyi sy = new shiyi();//sy.show();//匿名对象new shiyi();}
}直接new出一个对象,而他调用方法是这样调用的
class shiyi{
public void show() {System.out.println("调用了show方法");}
}
public class test {
public static void main(String[] args) {//带名字的调用//shiyi sy = new shiyi();//sy.show();//匿名对象new shiyi();//匿名对象调用方法new shiyi().show();}
}
我们平常所用的是这样的
而匿名对象:
改进版
本文发布于:2024-01-30 21:07:44,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170662006722851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |