会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134087个问题

打印出来为什么是字母隔断两个呢?逻辑有问题?

Printer:

package cn.sxt.thread;

public class Printer {
	private  int index=1;
	public  synchronized void print(int number) throws InterruptedException{
		if(index%3==0){
			super.wait();			
		}else{			
			System.out.println(number);
			super.notifyAll();
			index++;
	}
		
	}
	public  synchronized void print(char ch) throws InterruptedException{
		if(index%3!=0){
			super.wait();
		}else{
			System.out.println(ch);
			super.notifyAll();
			index++;
		}
	}

}

CharPrinter:

package cn.sxt.thread;

public class CharPrinter implements Runnable {
	private Printer printer;
	public CharPrinter(Printer printer){
		this.printer=printer;
	}
	@Override
	public void run() {
		for(char i='A';i<='Z';i++){
			try {
				printer.print(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	

}

NumberPrinter :

package cn.sxt.thread;

public class NumberPrinter implements Runnable {
	private Printer printer;
	public NumberPrinter(Printer printer){
		this.printer=printer;
	}

	@Override
	public void run() {
		for(int i=1;i<=52;i++){
			try {
				printer.print(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

}

TestThread

package cn.sxt.thread;

public class TestThread {
	public static void main(String[] args){
		Printer p = new Printer();
		NumberPrinter np = new NumberPrinter(p);
		CharPrinter cp = new CharPrinter(p);
		Thread t1 = new Thread(np);
		Thread t2 = new Thread(cp);
		t1.start();
		t2.start();
	}

}

image.png

MyNewThread.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 38311楼
Python全系列/第一阶段:AI驱动的Python编程/编程基本概念 38312楼
Python全系列/第一阶段:AI驱动的Python编程/编程基本概念 38313楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 38316楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 38321楼

——————————————————

//客户端
package com.jingdong.www;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Test1 {
	public static void main(String[] args) throws Exception{
		System.out.println("客服端启动了");
		Socket client=new Socket("127.0.0.1",9999);
		//获得输出流
		OutputStream os=client.getOutputStream();
		os.write("你在吗".getBytes());
		
		//获得输入流
		InputStream is=client.getInputStream();
		byte[] flush=new byte[1024];
		int len=0;
		while((len=is.read(flush))!=-1) {
			System.out.println(new String (flush,0,len));
		}
		//关闭
		if(is!=null) {
			is.close();
		}
		
		if(os!=null) {
				os.close();
		}
		if(client!=null) {
			client.close();
		}
				
				
				
		
	}
}
服务器端
package com.jingdong.www;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test1 {
	public static void main(String[] args) throws Exception {
		System.out.println("服务器启动了——————————————");
		//创建服务器对象
		ServerSocket server=new ServerSocket(9999);
		//监听
		Socket client=server.accept();
		
		//获得输入流
		InputStream is=client.getInputStream();
		byte[] flush=new byte[1024];
		int len=0;
		while((len=is.read(flush))!=-1) {
			System.out.println(new String (flush,0,len));
		}
		
		//获得输出流
		OutputStream os=client.getOutputStream();
		os.write("收到了!".getBytes());
		
		//关闭
		if(os!=null) {
			os.close();
		}
		
		if(is!=null) {
			is.close();
		}
		if(client!=null) {
			client.close();
		}
		
		
		
		
		
		
	}
}

老师,客服端发消息正常,服务器收消息正常,服务器发消息正常

但是客户端收不到服务器端的消息。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 38322楼

class Animal {
    public void shout() {
        System.out.println("叫了一声!");
    }
}
class Dog extends Animal {
    public void shout() {
        System.out.println("旺旺旺!");
    }
    public void seeDoor() {
        System.out.println("看门中....");
    }
}
class Cat extends Animal {
    public void shout() {
        System.out.println("喵喵喵喵!");
    }
}
public class TestPolym {
    public static void main(String[] args) {
        Animal a1 = new Cat(); // 向上可以自动转型
        //传的具体是哪一个类就调用哪一个类的方法。大大提高了程序的可扩展性。
        animalCry(a1);
        Animal a2 = new Dog();
        animalCry(a2);//a2为编译类型,Dog对象才是运行时类型。
         
        //编写程序时,如果想调用运行时类型的方法,只能进行强制类型转换。
        // 否则通不过编译器的检查。
        Dog dog = (Dog)a2;//向下需要强制类型转换
        dog.seeDoor();
    }
 
    // 有了多态,只需要让增加的这个类继承Animal类就可以了。
    static void animalCry(Animal a) {
        a.shout();
    }
 
    /* 如果没有多态,我们这里需要写很多重载的方法。
     * 每增加一种动物,就需要重载一种动物的喊叫方法。非常麻烦。
    static void animalCry(Dog d) {
        d.shout();
    }
    static void animalCry(Cat c) {
        c.shout();
    }*/
}

老师,这是学习材料里的代码,我可以理解它的意思但是我不理解为什么要引入多态,对于这个例子为什么不直接使用

        Dog d = new Dog();
        Cat c = new Cat();
        d.shout();
        c.shout();

这样的调用来实现,不需要重载动物的喊叫方法啊

JAVA 全系列/第一阶段:AI驱动的JAVA编程/面向对象详解和JVM底层内存分析 38323楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637