创建对象
class XiyoujiRenwu{
float height,weight;
String head,ear;
void speak(String s) {
System.out.println(s);
}
}
public class E {
public static void main(String args[]) {
XiyoujiRenwu zhubajie; //声明对象
zhubajie = new XiyoujiRenwu();//为对象分配变量(使用new和默认的构造方法)
}
}
class Point{
int x,y;
Point(int a,int b){
x=a;
y=b;
}
}
public class E {
public static void main(String args[]) {
Point p1,p2; //声明对象p1和p2
p1=new Point(10,10); //为对象p1分配变量(使用new和类中构造方法)
p2=new Point(23,35);//为对象p2分配变量(使用new和类中构造方法)
}
}
使用对象
package E;
class XiyoujiRenwu{
float height,weight;
String head,ear;
void speak(String s) {
head="歪着头";
System.out.println(s);
}
}
public class E {
public static void main(String args[]) {
XiyoujiRenwu zhubajie,sunwukong; //声明对象
zhubajie=new XiyoujiRenwu(); //为对象分配变量
sunwukong=new XiyoujiRenwu();
zhubajie.height=1.80f; //对象给自己的变量赋值
zhubajie.head="大头";
zhubajie.ear="一双大耳朵";
sunwukong.height=1.62f; //对象给自己的变量赋值
sunwukong.weight=1000f;
sunwukong.head="秀发飘飘";
System.out.println("zhubajie的身高: "+zhubajie.height);
System.out.println("zhubajie的头: "+zhubajie.head);
System.out.println("sunwukong的重量: "+sunwukong.weight);
System.out.println("sunwukong的头: "+sunwukong.head);
zhubajie.speak(" 俺老猪我想取媳妇"); //对象调用方法
System.out.println("zhubajie现在的头: "+zhubajie.head);
sunwukong.speak("老孙我重1000斤,我想骗八戒背我"); //对象调用方法
System.out.println("sunwukong现在的头: "+sunwukong.head);
}
}
对象的引用和实体
package E;
class Point{
int x,y;
void setXY(int a,int b){
x=a;
y=b;
}
}
public class E {
public static void main(String args[]) {
Point p1,p2;
p1=new Point();
p2=new Point();
System.out.println("p1的引用: "+p1);
System.out.println("p2的引用: "+p2);
p1.setXY(1111, 2222);
p2.setXY(-100, -200);
System.out.println("p1的x,y坐标:"+p1.x+","+p1.y);
System.out.println("p2的x,y坐标:"+p2.x+","+p2.y);
p1=p2;
System.out.println("将p2的引用赋值给p1后 :");
System.out.println("p1的引用:"+p1);
System.out.println("p2的引用:"+p2);
System.out.println("p1的x,y坐标:"+p1.x+","+p1.y);
System.out.println("p2的x,y坐标:"+p2.x+","+p2.y);
}
}
类与程序的基本结构
package E;
public class Rect {
double width;
double height;
double getArea() {
double area=width*height;
return area;
}
}
package E;
public class Lader {
double above;
double bottom;
double height;
double getArea() {
return (above+bottom)*height/2;
}
}
package E;
public class E {
public static void main(String args[]) {
Rect ractangle=new Rect();
ractangle.width=109.87;
ractangle.height=25.18;
double area=ractangle.getArea();
System.out.println("矩形的面积:"+area);
Lader lader=new Lader();
lader.above=10.798;
lader.bottom=156.65;
lader.height=18.12;
double aera1=lader.getArea();
System.out.println("梯形的面积:"+aera1);
}
}
参数传值
package E;
class Computer{
int add(int x,int y) {
return x+y;
}
}
public class E {
public static void main(String args[]) {
Computer com =new Computer();
int m=100;
int n=200;
int result=com.add(m, n); //将m,n的值船只给参数x,y
System.out.println(result);
result=com.add(120+m, n*10+8); //将表达式120+m和n*10+8的值传值给参数x,y
System.out.println(result);
}
}
引用类型参数的传值
package E;
public class Radio {
void openRadio(Battery battery) {
battery.electricityAmount=battery.electricityAmount-10; //消耗电量
}
}
package E;
public class E {
public static void main(String args[]) {
Battery nanfu=new Battery(100); // 创建电池对象
System.out.println("南孚电池的储电量是: "+nanfu.electricityAmount);
Radio radio=new Radio();
System.out.println("收音机开始使用南孚电池");
radio.openRadio(nanfu);
System.out.println("现在南孚电池的储电量是:"+nanfu.electricityAmount);
}
}
package E;
public class Battery {
int electricityAmount;
Battery(int amount){
electricityAmount=amount;
}
}