实验一:类和对象【实验目的】1.理解对象和类,掌握类的定义及如何创建一个对象;2.掌握构造方法的运用;3.掌握方法的定义和参数传递;4
掌握类的继承机制
【实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容】1.编写一个程序,显示水果的定购行情
定义一个带有参数的构造方法,这些参数用于存放产品名,数量和价格
在主程序中输出三种不同的水果
packagefruit;publicclassFruits{Stringgoodsname;intgoodsamount;doublegoodsprice;publicFruits(Stringgoodsname,intgoodsamount,doublegoodsprice){this
goodsname=goodsname;this
goodsamount=goodsamount;this
goodsprice=goodsprice;}publicvoidshowinfo(){System
println("产品名为"+this
goodsname);System
println("产品数量为"+this
goodsamount);System
println("产品价格为"+this
goodsprice);}}packagefruit;publicclassFruit{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubFruitsf=newFruits("apple",2,4);f
showinfo();Fruitsf1=newFruits("banana",2,3);f1
showinfo();Fruitsf2=newFruits("peach",2,2);f2
showin