In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge points of what the three commonly used interfaces of Java SE object-oriented programming are, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "what are the three common interfaces of Java SE object-oriented programming".
1.Comparable
Preface, want to sort Student. There are codes:
Import java.util.Arrays; class Student {public int age; public String name; public double score; public Student (int age, String name, double score) {this.age = age; this.name = name; this.score = score } @ Override public String toString () {return "Student {" + "age=" + age + ", name='" + name +'\'+ ", score=" + score +'}';}} public class TestDemo {public static void main (String [] args) {Student [] students = new Student [3] Students [0] = new Student (12, "niubi", 99.9); students [1] = new Student (20," liuren ", 18.9); students [2] = new Student (80,50.9); System.out.println (Arrays.toString (students)); Arrays.sort (students); System.out.println (Arrays.toString (students));}}
This code runs with an error:
Reason: did not tell how to sort, age or name or score. There are no rules to tell the comparison
Solution:
If you compare the size of a custom data type, be sure to implement a comparable interface.
Import java.util.Arrays; class Student implements Comparable {public int age; public String name; public double score; public Student (int age, String name, double score) {this.age = age; this.name = name; this.score = score } @ Override public String toString () {return "Student {" + "age=" + age + ", name='" + name +'\'+ ", score=" + score +'}' } / / whoever invokes this method is this @ Override public int compareTo (Student o) {/ / return this.age-o.ageAccord / return o.age-this.age;// from small to small}} public class TestDemo {public static void main (String [] args) {Student [] students = new Student [3] Students [0] = new Student (12, "niubi", 99.9); students [1] = new Student (6, "liuren", 18.9); students [2] = new Student (80, "laoren", 50.9); System.out.println ("before comparison" + Arrays.toString (students)); Arrays.sort (students) / / sort System.out.println from smallest to largest by default (after comparison + Arrays.toString (students));}}
If you want to compare scores with names,
/ / whoever invokes this method is this @ Override public int compareTo (Student o) {/ / return this.age-o.ageX / from small to big / / return o.age-this.age;// from big to small return (int) (this.score-o.score); / / score sort return this.name.compareTo (o.name); / / name sort}
Disadvantages: this interface is very intrusive to classes. Once you have written it, you dare not change it easily.
How to reduce the intrusiveness to the class?
Use Comparator
2.Comparator comparator import java.util.Arrays;import java.util.Comparator; class Student1 {public int age; public String name; public double score; public Student1 (int age, String name, double score) {this.age = age; this.name = name; this.score = score } @ Override public String toString () {return "Student {" + "age=" + age + ", name='" + name +'\'+ ", score=" + score +'}';}} class AgeComparator implements Comparator {@ Override public int compare (Student1 o1, Student1 O2) {return o1.age-o2.age }} class ScoreComparator implements Comparator {@ Override public int compare (Student1 o1, Student1 O2) {return (int) (o1.score-o2.score);}} class NameComparator implements Comparator {@ Override public int compare (Student1 o1, Student1 O2) {return o1.name.compareTo (o2.name);}} public class TestDemo1 {public static void main (String [] args) {Student1 [] students1 = new Student1 [3] Students1 [0] = new Student1 (12, "niubi", 99.9); students1 [1] = new Student1 (6," liuren ", 18.9); students1 [2] = new Student1 (80," laoren", 50.9); System.out.println ("before comparison" + Arrays.toString (students1)); AgeComparator ageComparator = new AgeComparator (); Arrays.sort (students1,ageComparator) System.out.println (after comparison (by age) + Arrays.toString (students1)); ScoreComparator scoreComparator = new ScoreComparator (); Arrays.sort (students1,scoreComparator); System.out.println (after comparison (by name) + Arrays.toString (students1)); NameComparator nameComparator = new NameComparator (); Arrays.sort (students1,nameComparator) System.out.println ("compare (by score)" + Arrays.toString (students1));}}
Running result:
Advantages: the intrusiveness to the class is very weak.
3.Cloneable
Interview questions:
Do you know the Cloneable interface? Why is this interface an empty interface? What's the use?
Empty interface-> flag interface-> indicates that the current class can be cloned.
Class Person implements Cloneable {public int age; public void eat () {System.out.println ("eat!");} @ Override public String toString () {return "Person {" + "age=" + age +'}';} @ Override protected Object clone () throws CloneNotSupportedException {return super.clone () }} public class TestDemo2 {public static void main (String [] args) throws CloneNotSupportedException {Person person = new Person (); person.age = 99; Person person2 = (Person) person.clone (); System.out.println (person.age); System.out.println (person2.age); System.out.println ("="); person2.age = 1999; System.out.println (person.age) System.out.println (person2.age);}}
Running result:
Note:
1. If the referenced object is to be cloned, it must implement the Cloneable interface.
two。 You must override the clone method and declare an exception.
This is the end of the introduction on "what are the three common interfaces of Java SE object-oriented programming". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.