packagecom.azurice;// 声明一个 Good 类classGood{intid;intstock;floatprice;}publicclassMain{publicstaticvoidmain(String[]args){System.out.println("HelloWorld!");}}
packagecom.azurice;// 声明一个 Good 类classGood{intid;intstock;floatprice;}publicclassMain{publicstaticvoidmain(String[]args){// 声明了一个 Good 类的变量Goodg;System.out.println(g);// 编译器报错,变量未初始化}}
packagecom.azurice;// 声明一个 Good 类classGood{intid;intstock;floatprice;}publicclassMain{publicstaticvoidmain(String[]args){// 声明了一个 Good 类的对象Goodg;g=newGood();System.out.println(g);}}
packagecom.azurice;// 声明一个 Good 类classGood{intid;intstock;floatprice;}publicclassMain{publicstaticvoidmain(String[]args){// 声明了一个 Good 类的对象Goodg;g=newGood();g.id=1;g.stock=7;g.price=9.96f;System.out.println(g.id);System.out.println(g.stock);System.out.println(g.price);}}
#include<stdio.h>#include<stdlib.h>// 声明一个 Good 结构体structGood{intid;intstock;floatprice;};intmain(){// 声明了一个 Good 结构体的指针structGood*g;g=(Good*)malloc(sizeof(structGood));g->id=1;g->stock=7g->price=9.96;printf("%d\n",g->id);printf("%d\n",g->stock);printf("%f\n",g->price);return0;}
publicstaticvoidmain(String[]args){Goodg;g=newGood();g.id=1;g.stock=7;g.price=9.96f;g.printInfo();if(!g.sell(8)){System.out.println("Don't have enough stock");}else{System.out.println("Sell success");}g.printInfo();if(!g.sell(2)){System.out.println("Don't have enough stock");}else{System.out.println("Sell success");}g.printInfo();}