java喂狗
❶ java定义一个动物类 Dog 有属性 行为 == 求详细步骤 谢谢 很急很急 在线等。
package com.java;
/声明一个抽象类动物
abstract class Animal {
//声明一个方法gnaw()啃骨头
public void gnaw() {
System.out.println("Animal gnaw(啃骨头)..");
}
//喝汤
public void eat() {
System.out.println("Animal eat(喝汤)...");
}
//咬人
public void bite() {
System.out.println("Animal bite(咬人)...");
}
//跑
public void run() {
System.out.println("Animal run(咬人)...");
}
//跳
public void jump() {
System.out.println("Animal jump...");
}
}
//声明一个Dog类,继承Animal类
class Dog extends Animal{
private String name;
private double weight;
private double height;
private String color;
public Dog (String name,String color){
this.name=name;
this.color=color;
}
}
//以下是测试结果,可以省略
public class NLL {
public static void main(String[] args) {
Animal smallDog =new Dog("smallDog","yellow");
Animal bigDog = new Dog("bigDog ","yellow");
smallDog.gnaw();
smallDog.eat();
smallDog.bite();
bigDog .bite();
}
}
❷ Java 狗狗类
publicclassDog{
/**
*昵称
*/
privateStringnickname;
/**
*品种
*/
privateStringtype;
/**
*颜色
*/
privateStringcolor;
publicvoidselfIntroction(){
System.out.println("Dog{"+
"昵称='"+nickname+'''+
",品种='"+type+'''+
",颜色='"+color+'''+
'}');
}
publicIntegerspeed(){
//不清楚具体需求可额外设置个属性返回该属性或在该方法中写自己的计算公式
return1;
}
publicDog(){
}
publicDog(Stringnickname,Stringtype,Stringcolor){
this.nickname=nickname;
this.type=type;
this.color=color;
}
publicStringgetNickname(){
returnnickname;
}
publicvoidsetNickname(Stringnickname){
this.nickname=nickname;
}
publicStringgetType(){
returntype;
}
publicvoidsetType(Stringtype){
this.type=type;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
}
❸ java建立猫cat类和狗类,具备叫声的方法cry(),
/*定义动物父类*/
classAnimal{
voidcry(){
System.out.println("Animalcry");
}
}
classCatextendsAnimal{
voidcry(){
System.out.println("catcry");
}
}
classDogextendsAnimal{
voidcry(){
System.out.println("dogcry");
}
}
publicclassExe2{
publicstaticvoidcry(Animalanimal){
animal.cry();
}
publicstaticvoidmain(String[]args){
Animalanimal=newAnimal();
cry(animal);
Catcat=newCat();
cry(cat);
Dogdog=newDog();
cry(dog);
}
}
❹ JAVA代码主人喂宠物吃东西狗只吃骨头猫只吃鱼求代码用多态
/*
animal是个抽象方法,Cat和Dogextends这个就是用的多态
*/
packageTest;
publicclassTest{
publicstaticvoidmain(String[]args){
Feederfeeder=newFeeder();
feeder.feedAnimals();
}
}
abstractclassAnimal{
publicabstractvoideat(Strings);
}
classDogextendsAnimal{
privatefinalStringFOOD="bone";
@Override
publicvoideat(Strings){
if(s==FOOD)
System.out.println("Dogiseatingbones");
else
System.out.println("Not"+this.FOOD+",Dogdon'twanttoeat");
}
}
classCatextendsAnimal{
privatefinalStringFOOD="fish";
@Override
publicvoideat(Strings){
if(s==FOOD)
System.out.println("Catiseatingfishes");
else
System.out.println("Not"+this.FOOD+",Catdon'twanttoeat");
}
}
classFeeder{
privatefinalString[]FOODS={"fish","bone","shit"};
privateAnimalcat;
privateAnimaldog;
Feeder(){
dog=newDog();
cat=newCat();
}
publicvoidfeedAnimals(){
System.out.println("Feedinganimals...");
Stringfood;
for(inti=0;i<FOODS.length;i++){
food=FOODS[i];
if(food=="fish")
this.cat.eat(food);
elseif(food=="bone")
this.dog.eat(food);
else{
System.out.println("NotFishesorBones,is"+food);
}
}
System.out.println("Done!");
}
}
❺ java 线程 唤醒问题
首先,从你程序的本意来看, 你是想用线程实现一个生产者-- 消费者模式, 用在馒头的场景, 如下:
1、 肯定需要一个篮子, 负责装馒头,并且这个篮子有容量,假设容量为C;
2、 有两个人,第一个是爹(生产者), 第二个是娃(消费者), 爹负责向篮子中放入馒头, 娃负责从篮子中取出馒头喂狗;
3、 但是篮子的容量是有限的, 当篮子被装满时, 爹就等待娃从篮子中取出馒头,篮子腾出空间之后,爹继续装馒头; 当篮子的馒头都被取走后, 娃就等待, 当篮子中又被放入馒头时,才继续取馒头。
附上代码:
篮子:
public class Bucket {
private int count = 0; // 篮子能够容纳的馒头数
private int total = 0; // 放馒头的总数
public synchronized void put(){
if(count == 5){
try {
System.out.println("俺是他爹,篮子满了,俺在等俺家娃拿馒头喂狗");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count += 1;
total++;
System.out.println("俺是他爹,俺放了一个馒头,现有篮子里有 [" + count + "] 个馒头");
notify();
}
public synchronized boolean get(){
if(count == 0){
try {
wait();
System.out.println("俺是他娃,篮子空了,俺在等俺爹放馒头到篮子里");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count -= 1;
System.out.println("俺拿了一个馒头喂俺家大花狗, 篮子里还有 [" + count + "] 个馒头");
notify();
if(total == 100 && count == 0){
return false;
}else{
return true;
}
}
爹:
public class Proct implements Runnable{
private Bucket bucket;
Proct(Bucket bucket){
this.bucket = bucket;
}
@Override
public void run() {
for(int i=0; i<100; i++){
bucket.put();
}
System.out.println("俺把馒头都放完了");
}
}
娃:
public class Consumer implements Runnable{
private Bucket bucket;
public Consumer(Bucket bucket){
this.bucket = bucket;
}
@Override
public void run() {
while(true){
if(!bucket.get()){
break;
}
}
System.out.println("俺家大花狗吃完馒头了");
}
}
启动类:
public class Test {
public static void main(String[] args) {
Bucket bucket = new Bucket();
new Thread(new Consumer(bucket)).start();
new Thread(new Proct(bucket)).start();
}
}
最后附上你的问题, notify 由谁来唤醒:
注意 wait() 和 notify() 是Object类上的方法,
notify() 的意思是: 从加锁的对象的监视器(监视器就是锁)的等待队列中, 任意取出一个等待线程, 让该线程处于runnable状态;
wait()的意思是: 把锁住对象的当前线程, 放入到监视器的等待队列中。
所以,notify()的意思就是要唤醒等待队列中的一个等待线程,当程序发起这么一个事件后, 是由虚拟机的线程调度器完成线程状态之间的转换的。
❻ 用Java程序完成以下场景(用继承多态):有一个主人(Master类),他养了两只宠物(Pet类)
publicclassRun{
publicstaticvoidmain(String[]args){
Mastermaster=newMaster();
master.feedDog("鸡骨头");
master.feedCat("鸡骨头");
}
}
classMaster{
privatePetmPet;
privateFoodmFood;
publicvoidfeedCat(Stringfood){
mPet=newCat();
mFood=newFood(food);
mPet.eat(mFood);
}
publicvoidfeedDog(Stringfood){
mPet=newDog();
mFood=newFood(food);
mPet.eat(mFood);
}
}
classDogextendsPet{
@Override
publicvoideat(Foodfood){
System.out.println("正在喂小狗吃"+food.getFood());
if(food.getFood().matches(Food.BONE)){
System.out.println("小狗正在吃"+food.getFood()+"!");
}else{
System.out.println("但是小狗不喜欢吃"+food.getFood()+"!");
}
}
}
classCatextendsPet{
@Override
publicvoideat(Foodfood){
System.out.println("正在喂小猫吃"+food.getFood());
if(food.getFood().matches(Food.FISH)){
System.out.println("小猫正在吃"+food.getFood()+"!");
}else{
System.out.println("但是小猫不喜欢吃"+food.getFood()+"!");
}
}
}
classFood{
publicfinalstaticStringBONE=".*骨.*";
publicfinalstaticStringFISH=".*鱼.*";
privateStringfood;
publicStringgetFood(){
returnfood;
}
publicvoidsetFood(Stringfood){
this.food=food;
}
publicFood(Stringfood){
this.food=food;
}
}
classPet{
publicvoideat(Foodfood){
}
}