java super.getClass().getName()得到了什么

也是无意间在论坛中发现的一个问题,就像一个脑筋急转弯。题目如下:

public class b1 extends Date {
public static void main(String[] args){
new b1().test();
}
public void test(){
System.out.println(super.getClass().getName());
}
}

这个东西结果是什么。

初看这东西,我只能说,输出:Date。但亲自敲代码试了后,发现输出b1。

是啥原因呢?

原来getClass()返回的是当前运行类的class名字,当前运行的类当然不会是Date了。所以super.getClass().getName()就相当于getClass().getName()。故输出为b1。

如果想得到父类classname。用getClass().getSuperclass().getName()即可。

/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
在 Test1 类中,你定义的 Books 类应该是一个具体的类而不是抽象类,因为你在 main 方法中实例化了它。此外,你在 compareTo 方法中返回了固定的值 1,这将导致 TreeSet 不会正确排序。你需要根据主要条件和次要条件来实现 compareTo 方法的逻辑。最后,你可以考虑在 Books 类中实现 hashCode 和 equals 方法,以确保在 Set 中正确处理对象。 下面是修改后的代码: ``` 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<Books> set = new HashSet<>(); TreeSet<Books> tree = new TreeSet<>(); Books books1 = new Books(01, "坤坤成长史", 520, "我的心"); Books books2 = new Books(02, "坤坤帅哥", 1314, "我的脑子"); Books books3 = new Books(03, "双开门大冰箱", 999, "我的幻想"); Books books4 = new Books(01, "坤坤成长史", 520, "我的心"); set.add(books1); set.add(books2); set.add(books3); set.add(books4); tree.add(books1); tree.add(books2); tree.add(books3); tree.add(books4); System.out.println("HashSet:"); for(Books s1 : set){ System.out.println(s1.getNumber() + s1.getName() + s1.getPrice() + s1.getPublisher()); } System.out.println("TreeSet:"); for(Books s2 : tree){ System.out.println(s2.getNumber() + s2.getName() + s2.getPrice() + s2.getPublisher()); } } static class Books implements Comparable<Books> { private int number; private String name; private double price; private String publisher; public Books(int number, String name, double price, String publisher){ 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 compareTo(Books o) { if (this.price != o.price) { return Double.compare(this.price, o.price); } else if (!this.name.equals(o.name)) { return this.name.compareTo(o.name); } else { return Integer.compare(this.number, o.number); } } @Override public int hashCode() { return Objects.hash(number, name, price, publisher); } @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); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值