java»ù´¡ÑµÁ· ÏÂÔر¾ÎÄ

ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2025/2/23 4:29:56ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£

}

}

}

return false; //Èç¹û²»ÊÇ,ÔòÖ±½Ó·µ»Øfalse

Persona per=(Persona)obj;//½«´«½øÀ´µÄ¶ÔÏóÏòÏÂתÐÍ

if(per.name.equals(this.name)&&per.age==this.age){ }

return true; return false; }else{

public String toString(){ }

return \ÐÕÃû£º\+this.name+\ÄêÁ䣺\+this.age;

public class ObjectDemo1 { }

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

Persona per1=new Persona(\СÃ÷\,20); Persona per2=new Persona(\Сǿ\,22); Persona per3=new Persona(\Сǿ\,22);

System.out.println(per1.equals(per2)?\ÊÇͬһ¸öÈË£¡\:\²»ÊÇͬһ¸öÈË£¡System.out.println(per2.equals(per3)?\ÊÇͬһ¸öÈË£¡\:\²»ÊÇͬһ¸öÈË£¡System.out.println(per1.equals(\)?\ÊÇͬһ¸öÈË£¡\:\²»ÊÇͬһ¸ö

\); \); ÈË£¡\);

ÏÔʾ½á¹û£º

²»ÊÇͬһ¸öÈË£¡ ÊÇͬһ¸öÈË£¡ ²»ÊÇͬһ¸öÈË£¡

Ò»¡¢Ê¹ÓÃÃæÏò¶ÔÏóµÄ¸ÅÄî±íʾ³öÏÂÃæµÄÉú»î³¡¾°£º

СÃ÷È¥³¬ÊÐÂò¶«Î÷£¬ËùÓÐÂòµ½µÄ¶«Î÷¶¼·ÅÔÚÁ˹ºÎï³µÖ®ÖУ¬×îºóµ½ÊÕÒø̨һÆð½áÕË¡£

class Customer{

private String name;

public Customer(String name){ this.name=name;

System.out.println(\¹Ë¿ÍÐÕÃû£º\+name); } }

class Goods{

private String gname; private double price;

}

private static float countPrice; private static int countGoods;

public Goods(String gname,double price ){ }

public String getGname(){ }

public double getPrice(){ }

public void getGood(){ }

public String getCount(){ }

return \ÉÌÆ·×ÜÊý£º\+countGoods+\ÉÌÆ·×ܶ\+countPrice+\Ôª\; System.out.println(\ÉÌÆ·Ãû³Æ£º\+gname+\ÉÌÆ·¼Û¸ñ\+price+\Ôª\); return price; return gname; this.gname=gname; this.price=price; countPrice+=price; countGoods++;

class Cart{ }

public class Buy {

public static void main(String[] args) {

Customer cus=new Customer(\СÃ÷\); private Goods good; public Cart(Goods good){ }

public void getInfo(){

System.out.println(good.getCount()); }

this.good=good;

Goods good1=new Goods(\Ãæ°ü\,2.00); good1.getGood();

Goods good2=new Goods(\Æ»¹û\,4.20); good2.getGood();

Goods good3=new Goods(\¿ÉÀÖ\,3.00); good3.getGood();

Cart cart=new Cart(good3); cart.getInfo(); }

}

ÏÔʾ½á¹û£º

¹Ë¿ÍÐÕÃû£ºÐ¡Ã÷

ÉÌÆ·Ãû³Æ£ºÃæ°ü,ÉÌÆ·¼Û¸ñ2.0Ôª ÉÌÆ·Ãû³Æ£ºÆ»¹û,ÉÌÆ·¼Û¸ñ4.2Ôª ÉÌÆ·Ãû³Æ£º¿ÉÀÖ,ÉÌÆ·¼Û¸ñ3.0Ôª ÉÌÆ·×ÜÊý£º3, ÉÌÆ·×ܶ9.2Ôª

1. Òì³£´¦Àí¡£

public class Exception { }

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

System.out.println(\¼ÆË㿪ʼ****\); int i=0; int j=0; try{ }

catch(ArithmeticException e){ }

catch(NumberFormatException e){ }

catch(ArrayIndexOutOfBoundsException e){ }

finally{

System.out.println(\²»¹ÜÊÇ·ñ³öÏÖÒì³££¬´Ë¾ä¶¼½«Ö´ÐУ¡\); }

System.out.println(\Êý×éÔ½½çÒì³££º\+e); System.out.println(\Êý×Öװת»»Òì³££º\+e); System.out.println(\ËãÊýÒì³££º\+e); String str1=args[0]; String str2=args[1]; i=Integer.parseInt(str1); j=Integer.parseInt(str2); int temp=i/j;

System.out.println(\Á½¸öÊý×ÖÏà´¦µÄ½á¹ûÊÇ£º\+temp); System.out.println(\);

2. throws ¹Ø¼ü×Ö ´¦ÀíÒì³£¡£Ê¹ÓÃthrows ÉùÃ÷µÄ·½·¨±íʾ´Ë·½·¨²»´¦ÀíÒì³££¬

¶ø½»¸ø·½·¨µÄµ÷Óô¦½øÐд¦Àí¡£

class Math{ }

public class Exception1 {

public static void main(String[] args) {

Math m=new Math();

try{ //ÒòΪÓÐthrows £¬²»¹ÜÊÇ·ñÓÐÒì³££¬¶¼±ØÐë´¦Àí System.out.println(\³ý·¨²Ù×÷£º\+m.div(10, 2)); }

public int div(int i,int j)throws Exception{ //´Ë·½·¨Öв»´¦ÀíÒì³£ int temp=i/j; }

return temp;

catch(Exception e){

e.printStackTrace(); //´òÓ¡Òì³£ } }

ÏÔʾ½á¹û£º

}

³ý·¨²Ù×÷£º5 ¡¶¶þ¡·

class Math2{ }

public class Exception2 { }

ÏÔʾ½á¹û£º ³ý·¨²Ù×÷£º5

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

Math2 m=new Math2();

System.out.println(\³ý·¨²Ù×÷£º\+m.div(10, 2)); public int div(int i,int j)throws Exception{ }

int temp=i/j; return temp;

3. throw ¹Ø¼ü×ÖÅ׳öÒ»¸öÒì³££¬Å׳öʱֱ½ÓÅ׳öÒì³£ÀàµÄʵÀý»¯¶ÔÏó¼´¿É¡£

public class Exception3 { }

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

try{

throw new Exception(\×Ô¼ºÅ׳öµÄÒì³£!\); }catch(Exception e){ }

System.out.println(e);

ÏÔʾ½á¹û£º

java.lang.Exception: ×Ô¼ºÅ׳öµÄÒì³£! 4. ·¶Àý¡ª¡ªthrow class Math3{

public int div(int i,int j) throws Exception{

System.out.println(\¼ÆË㿪ʼ****\); int temp =0; try{ }

return temp;

temp=i/j;

throw e; //°ÑÒì³£½»¸ø±»µ÷Óô¦

System.out.println(\¼ÆËã½áÊø****\); }catch(Exception e){ }finally{

ÓëthrowsµÄÓ¦Óá£