super.getClass().getName()


/*  从一段代码,侧面说明super.getClass().getName()打印结果的匪夷所思之处。 super.getClass().getName() //打印当前运行时类名 this.getClass().getName()//打印当前运行时类名 this.getClass().getSuperclass().getName()//打印当前运行时类的父类名  */ class SuperClass {  final void getString(){   System.out.println("now here");  } }

public class SubClass extends SuperClass {   public static void main(String[] args) {   new SubClass().test();  }

 public void test() {   System.out.println(super.getClass().getName());//按常理人们都会认为打印结果应该是SuperClass   System.out.println(this.getClass().getName());//这段代码是等效于上一段代码的,   System.out.println(this.getClass().getSuperclass().getName());//这段代码可以达到获取父类名称的功能   /*    * 解释:    * 1.概念解释:    * 在Object体系中,getClass()方法是被final修饰,    * 意味着任何继承Object或继承Object子类的类都不能重写此方法, 只能调用父类中的该方法,    * 既然都是调用父类中的方法,故super.getClass()和this.getClass()方法效果是一样的。

   * Java API中解释道:getClass()方法得到的类名是运行时的类,所以最终得到的还是当前运行类的类名。    *    * 2.实例解释:    * 在本例中,我们模仿了getClass()方法制造出了一个被final修饰的getString()方法,    * 并分别用super和this来调用,得到的结果均为父类中的getString的运行结果,其原因是子类并未重写该方法,    * 也无权重写(因为被final修饰了)。所以不管用super调用还是this调用,得到的结果都会是父类中的方法得出的结果。    *   */   super.getString();   this.getString();  } }

/import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.TreeSet; public class Test1 { public static void main(String[] args){ Set<Test.Books> set = new HashSet<>(); TreeSet<Test.Books> tree = new TreeSet<>(); Test.Books books1 = new Test.Books(01,"坤坤成长史",520,"我的心"); Test.Books books2 = new Test.Books(02,"坤坤帅哥",1314,"我的脑子"); Test.Books books3 = new Test.Books(03,"双开门大冰箱",999,"我的幻想"); Test.Books books4 = new Test.Books(01,"坤坤成长史",520,"我的心"); set.add(books1); set.add(books2); set.add(books3); set.add(books4); //TreeSet(): 根据其元素的自然排序进行排序 tree.add(books1); tree.add(books2); tree.add(books3); tree.add(books4); System.out.println("HashSet:"); for(Test.Books s1 : set){ System.out.println(s1.getNumber() + s1.getname() + s1.getPrice() +s1.getPublisher()); } System.out.println("TreeSet:"); for(Test.Books s2 : tree){ System.out.println(s2.getNumber() + s2.getname() + s2.getPrice() +s2.getPublisher()); } } //Set没有带索引的方法 //TreeSet 无参构造方法 自然排序 让元素所属的类实现Comparable接口,重写compareTo方法 //重写方法,注意主要条件和次要条件 static abstract class Books implements Comparable<Books> { private int number; private String name; private double price; private String publisher; public Books(){ } Books(int number,String name,double price,String publisher){ super(); this.number = number; this.name = name; this.price = price; this.publisher = publisher; } public int getNumber(){ return number; } public String getname(){ return name; } public double getPrice() { return price; } public String getPublisher() { return publisher; } @Override public int hashCode() { return 0; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Books books = (Books) o; return number == books.number && Double.compare(books.price, price) == 0 && Objects.equals(name, books.name) && Objects.equals(publisher, books.publisher); } public int compareTo(Books s2){ return 1; } } }请帮我修改
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值