==和equals浅谈
java面试题之equals和==的区别_好想去买菜的博客-CSDN博客
==比的是地址,equals比的具体值;
链表项目:(buy give search 功能为具体实现。)
部分代码:
interface ILink<T>{//链表接口
}
class LinkImpl<T> implements ILink<T>{//接口实现类
}
interface IBook{
public String getName();
public String getAuthor();
public double getPrice();
}
class MathBook implements IBook{
private String name;
private String author;
private Double price;
public MathBook(){
}
public MathBook(String name,String author,Double price){
this.name=name;
this.author=author;
this.price=price;
}
public String getName(){
return this.name;
}
public String getAuthor(){
return this.author;
}
public double getPrice(){
return this.price;
}
public String toString(){
return "书名:"+this.name+"、作者:"+this.author+"、价格"+this.price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {//地址相同
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof MathBook)){
return false;
}
MathBook book=(MathBook)obj;
return book.name.equals(this.name)&&book.author.equals(this.author)&&book.price.equals(this.price);
}
}
class BigDataBook implements IBook{
private String name;
private String author;
private Double price;
public BigDataBook(){
}
public BigDataBook(String name,String author,Double price){
this.name=name;
this.author=author;
this.price=price;
}
public String getName(){
return this.name;
}
public String getAuthor(){
return this.author;
}
public double getPrice(){
return this.price;
}
public String toString(){
return "书名:"+this.name+"、作者:"+this.author+"、价格"+this.price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {//地址相同
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof MathBook)){
return false;
}
BigDataBook book=(BigDataBook)obj;
return book.name.equals(this.name)&&book.author.equals(this.author)&&book.price.equals(this.price);
}
}
class ProgramBook implements IBook{
private String name;
private String author;
private Double price;
public ProgramBook(){
}
public ProgramBook(String name,String author,Double price){
this.name=name;
this.author=author;
this.price=price;
}
public String getName(){
return this.name;
}
public String getAuthor(){
return this.author;
}
public double getPrice(){
return this.price;
}
public String toString(){
return "书名:"+this.name+"、作者:"+this.author+"、价格"+this.price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {//地址相同
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof MathBook)){
return false;
}
ProgramBook book=(ProgramBook)obj;//向下转型,为了调用重写后的equals方法
return book.name.equals(this.name)&&book.author.equals(this.author)&&book.price.equals(this.price);
}
}
class Student{
private ILink<IBook> books=new LinkImpl<>() ;//链表装书
public void buy(IBook book){//买书
this.books.add(book);//向链表里保存
}
public void give(IBook book){//赠送
this.books.remove(book);//移除
}
public ILink search(String keyword){//检索
ILink<IBook> result=new LinkImpl<>();
Object []obj=this.books.toArray();//获取全部的链表数据
for(Object temp : obj){
IBook book=(IBook)temp;
if(book.getAuthor().contains(keyword)&&book.getName().contains(keyword)){
result.add(book);
}
}
return result;
}
}
public class demo322 {
public static void main(String[] args) {
Student student=new Student();
Student.buy(new BigDataBook("w","s",654.2));
}
}
Idea的基本操作:
进程和线程
/**
* 多线程错误示范
*/
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name=name;
}
@Override
public void run() {//线程运行的主方法run
for(int i=0;i<10;i++){
System.out.println("启动线程:"+this.name+"执行了"+i+"次");
}
}
}
public class demo322{
public static void main(String[] args) {
MyThread myThreadA=new MyThread("A");
MyThread myThreadB=new MyThread("B");
MyThread myThreadC=new MyThread("C");
myThreadA.run();
myThreadB.run();
myThreadC.run();
}
}
执行结果:
启动线程:A执行了0次
启动线程:A执行了1次
启动线程:A执行了2次
启动线程:A执行了3次
启动线程:A执行了4次
启动线程:A执行了5次
启动线程:A执行了6次
启动线程:A执行了7次
启动线程:A执行了8次
启动线程:A执行了9次
启动线程:B执行了0次
启动线程:B执行了1次
启动线程:B执行了2次
启动线程:B执行了3次
启动线程:B执行了4次
启动线程:B执行了5次
启动线程:B执行了6次
启动线程:B执行了7次
启动线程:B执行了8次
启动线程:B执行了9次
启动线程:C执行了0次
启动线程:C执行了1次
启动线程:C执行了2次
启动线程:C执行了3次
启动线程:C执行了4次
启动线程:C执行了5次
启动线程:C执行了6次
启动线程:C执行了7次
启动线程:C执行了8次
启动线程:C执行了9次
Process finished with exit code 0
/**
* 多线程正确示范
*/
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name=name;
}
@Override
public void run() {//线程运行的主方法run
for(int i=0;i<10;i++){
System.out.println("启动线程:"+this.name+"执行了"+i+"次");
}
}
}
public class demo322{
public static void main(String[] args) {
MyThread myThreadA=new MyThread("A");
MyThread myThreadB=new MyThread("B");
MyThread myThreadC=new MyThread("C");
myThreadA.start();
myThreadB.start();
myThreadC.start();
}
}
执行结果:
启动线程:A执行了0次
启动线程:B执行了0次
启动线程:B执行了1次
启动线程:B执行了2次
启动线程:B执行了3次
启动线程:B执行了4次
启动线程:C执行了0次
启动线程:B执行了5次
启动线程:A执行了1次
启动线程:B执行了6次
启动线程:C执行了1次
启动线程:C执行了2次
启动线程:C执行了3次
启动线程:B执行了7次
启动线程:A执行了2次
启动线程:A执行了3次
启动线程:A执行了4次
启动线程:A执行了5次
启动线程:A执行了6次
启动线程:A执行了7次
启动线程:A执行了8次
启动线程:A执行了9次
启动线程:B执行了8次
启动线程:B执行了9次
启动线程:C执行了4次
启动线程:C执行了5次
启动线程:C执行了6次
启动线程:C执行了7次
启动线程:C执行了8次
启动线程:C执行了9次
Process finished with exit code 0
总结:调用start()方法后会自动调用run()方法,并启动线程。
Runnable:
/**
* Runnable接口 启动线程 new Thread(myThreadA).start();
*/
class MyThread implements Runnable{
private String name;
public MyThread(String name){
this.name=name;
}
@Override
public void run() {//线程运行的主方法run
for(int i=0;i<10;i++){
System.out.println("启动线程:"+this.name+"执行了"+i+"次");
}
}
}
public class demo322{
public static void main(String[] args) {
MyThread myThreadA=new MyThread("A");
MyThread myThreadB=new MyThread("B");
MyThread myThreadC=new MyThread("C");
new Thread(myThreadA).start();
new Thread(myThreadB).start();
new Thread(myThreadC).start();
}
}
lambda表达式:
public class demo322{
public static void main(String[] args) {
for(int x=1;x<3;x++){
String name="A";
new Thread(()->{
for(int i=0;i<10;i++){
System.out.println("启动线程:"+name+"执行了"+i+"次");
}
}).start();
}
}
}
Thread threadA=new Thread(myThread) 此时,threadA指向的堆内存中是Runnable的对象实例target(因为Thread()方法)。内容是myThread的属性。