java基础训练 - 百度文库

System.out.println(\Ïß³ÌÔËÐСª¡ª>\+i); } }

}

ÏÔʾ½á¹ûÈçÏ£º£¨½á¹û²»Î¨Ò»£¬²»Ö¹ÒÔϽá¹û£¬½ö¸´ÖÆ´ËÈý¸ö×÷˵Ã÷£©

ËÄ¡¢ÅжÏÏß³ÌÊÇ·ñÆô¶¯

class MyThread implements Runnable{ }

public class ThreadAliveDemo {

public static void main(String[] args) { }

MyThread mt=new MyThread(); Thread t=new Thread(mt,\Ïß³Ì\);

System.out.println(\Ïß³ÌÖ´ÐÐ֮ǰ¡ª¡ª>\+t.isAlive()); t.start();

System.out.println(\Ïß³ÌÖ´ÐÐÖ®ºó¡ª¡ª>\+t.isAlive()); for(int i=0;i<3;i++){ }

System.out.println(\´úÂëÖ´ÐÐÖ®ºó¡ª¡ª>\+t.isAlive());

System.out.println(\ÔËÐСª¡ª>\+i);

public void run(){ }

for(int i=0;i<3;i++){ }

System.out.println(Thread.currentThread().getName()+\ÔËÐÐ

¡ª¡ª>\+i);

}

ÏÔʾ½á¹ûÈçÏ£¨½á¹û²»Öª´Ë¼¸¸ö£©£º

Ò»¡¢ Éè¼ÆÒ»¸ö³èÎïÉ̵꣬³èÎïÉ̵êÖпÉÒÔÓжàÖÖ£¨ÓÐÓû§¾ö¶¨ÊýÁ¿£©³èÎÊÔ±íʾ³ö

´ËÖÖ¹Øϵ£¬²¢ÒªÇó¿ÉÒÔ¸ù¾Ý³èÎïµÄ¹Ø¼ü×Ö²éѯµ½ÏàÓ¦µÄ³èÎïÐÅÏ¢¡£ËùÐè³èÎïÐÅÏ¢×ÔÐÐÉè

¼Æ¡£¾ßÌå·ÖÎöÈçÏ£º

£¨1£© ±¾ÒªÇóÖÐÌáʾ³èÎïÐÅÏ¢¿ÉÒÔ×ÔÐÐÉè¼Æ£¬ËùÒÔ´Ëʱ¼òµ¥Éè¼Æ³öÃû×Ö¡¢ÑÕÉ«¡¢ÄêÁä3

¸öÊôÐÔ¡£

£¨2£© ³èÎïµÄÖÖÀàºÜ¶à£¬Èçè¡¢¹·µÈ¶¼ÊôÓÚ³èÎËùÒÔ³èÎïÓ¦µ±ÊÇÒ»¸ö±ê×¼¡£ £¨3£© ÔÚ³èÎïÉ̵êÖУ¬Ö»ÒªÊÇ·ûºÏ´Ë³èÎï±ê×¼µÄ¾ÍÓ¦¸Ã¿ÉÒԷŽø³èÎïÉ̵êÖС£

£¨4£© ³èÎïÉ̵êÒª±£´æ¶àÖÖ³èÎÔò¿Ï¶¨Ó¦¸ÃÊdzèÎïµÄ¶ÔÏóÊý×飬Èç¹û³èÎï¸öÊýÓÉÓÃ

»§¾ö¶¨£¬ÔòÓ¦¸Ã´´½¨³èÎïÉ̵êʱ£¬¾Í·ÖÅäºÃËùÄܱ£´æµÄ³èÎï¸öÊý¡£

´úÂëÈçÏ£º

interface Pet{ }

class Cat implements Pet{

private String name; private String color; private int age;

public Cat(String name,String color,int age){ }

public String getName(){ }

public String getColor(){ }

return this.color; return this.name; this.setName(name); this.setColor(color); public String getName(); public String getColor(); public int getAge();

this.setAge(age);

}

public int getAge(){ }

public void setName(String name){ }

public void setColor(String color){ }

public void setAge(int age){ }

this.age=age; this.color=color; this.name=name; return this.age;

class Dog implements Pet{ }

class PetShop{

private Pet[] pets; private int foot; private String name;

private String color; private int age;

public Dog(String name,String color,int age){ }

public String getName(){ }

public String getColor(){ }

public int getAge(){ }

public void setName(String name){ }

public void setColor(String color){ }

public void setAge(int age){ }

this.age=age; this.color=color; this.name=name; return this.age; return this.color; return this.name; this.setName(name); this.setColor(color);

this.setAge(age);

}

public PetShop(int len){ }

public boolean add(Pet pet){ }

public Pet[] search(String keyWord){ //¹Ø¼ü×Ö²éÕÒ

Pet p[]=null; int count=0;

//È·ÈÏ¿ª±ÙµÄ¿Õ¼ä´óС£¬¿´ÓжàÉÙ¸ö³èÎï·ûºÏ²éѯÌõ¼þ for(int i=0;i

if(this.pets[i]!=null){ if(this.foot

this.pets[this.foot]=pet; this.foot++; return true; return false; if(len>0){ }

this.pets=new Pet[len];

this.pets=new Pet[1]; //ÖÁÉÙ¿ª±ÙÒ»¸ö¿Õ¼ä }else{

}else{

if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getC

}

p=new Pet[count];

int f=0;//ÉèÖÃÔö¼ÓµÄλÖñê¼Ç

for(int i=0;i

if(this.pets[i]!=null){ }

}

count++; //·ûºÏÌõ¼þµÄ³èÎï¸öÊý

olor().indexOf(keyWord)!=-1){

if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getC }

}

return p;

}

}

p[f]=this.pets[i];//½«·ûºÏ²éѯÌõ¼þµÄ³èÎïÐÅÏ¢±£´æ f++;

olor().indexOf(keyWord)!=-1){

public class PetShopDemo {

public static void main(String[] args) {

PetShop ps=new PetShop(5);

ps.add(new Cat(\°×è\,\°×É«µÄ\,2));//Ôö¼Ó³èÎ³É¹¦ ps.add(new Cat(\ºÚè\,\ºÚÉ«µÄ\,3));//Ôö¼Ó³èÎ³É¹¦ ps.add(new Cat(\»¨Ã¨\,\»¨É«µÄ\,3));//Ôö¼Ó³èÎ³É¹¦ ps.add(new Dog(\À­²¼À­¶à\,\»ÆÉ«µÄ\,3));//Ôö¼Ó³èÎ³É¹¦ ps.add(new Dog(\½ðë\,\½ðÉ«µÄ\,3));//Ôö¼Ó³èÎ³É¹¦ ps.add(new Dog(\»Æ¹·\,\»ÆÉ«µÄ\,3));//Ôö¼Ó³èÎʧ°Ü print(ps.search(\»Æ\)); }

}

public static void print(Pet p[]){

for(int i=0;i

if(p[i]!=null){

System.out.print(p[i].getName()+\+p[i].getAge()+\+p[i].getAge }

}

}

());

ÏÔʾ½á¹û£º

À­²¼À­¶à,3,3

¶þ¡¢ ¶ÔÏóµÄ±È½Ï

ObjectÀàÌṩµÄequals£¨£©·½·¨Ä¬ÈÏÊDZȽϵØÖ·µÄ£¬²¢²»ÄܶÔÄÚÈݽøÐбȽϡ£

class Persona{

private String name; private int age;

public Persona(String name,int age){ }

public boolean equals(Object obj){//¸²Ð´ObjectÀàÖеÄequals()·½·¨

if(this==obj){ //Èç¹ûÁ½¸ö¶ø¶ÔÏóµÄµØÖ·ÏàµÈ£¬Ôò¿Ï¶¨ÊÇͬһ¸ö }

if(!(obj instanceof Persona)){//Åжϴ«½øÀ´µÄ¶ÔÏóÊÇ·ñÊÇPersonµÄʵÀý

return true; this.name=name; this.age=age;

¶ÔÏó

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ì滻Ϊ@) ËÕICP±¸20003344ºÅ-4