322学习日记(==equals浅谈、链表项目、IDEA基本操作、进程和线程理解、lambda)

==和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的属性。

Thread(target)之run()方法_理想二旬的博客-CSDN博客_thread的target

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值