java基础训练 下载本文

内容发布更新时间 : 2024/5/18 9:15:01星期一 下面是文章的全部内容请认真阅读。

}

}

}

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的应用。