使用修饰符
public class Aaa {
public static int width; //静态变量
protected int height;
private int posx=0; //私有变量
public static int getWidth() {
return width;
}
public static void setWidth(int w) {
width=w;
}
public int getHeight() {
return height;
}
public void setHeight(int h) {
this.height=h;
}
}
public class Bbb {
public static void main(String[] args) {
System.out.println("静态变量x="+Aaa.getWidth());
// System.out.println("实例变量y="+Aaa.getHeight()); //非法,编译时出错
Aaa a=new Aaa();
Aaa b=new Aaa();
a.setWidth(24);
a.setHeight(20);
b.setHeight(3);
// a.posx=10; //非法,编译时出错
b.setHeight(6);
System.out.println("静态变量a.Width="+a.getWidth());
System.out.println("实例变量a.Height="+a.getHeight());
System.out.println("静态变量b.Width="+b.getWidth());
System.out.println("实例变量b.Height="+b.getHeight());
}
}
类中方法的传递
public class First {
public static void main(String[] args) {
int x=2,y=3;
setXY(x,y);
System.out.println("x="+x+",y="+y);
}
static void setXY(int XX,int YY) {
XX=XX++;
YY=YY*YY;
System.out.println("XX="+XX+",YY="+YY);
}
}
类的继承性
public class Aaa {
protected String name; //具有保护修饰符的成员变量
protected int age;
private String id;
void setInfo(String name,int age,String id) { //设置数据的方法
this.name=name;
this.age=age;
this.id=id;
}
void setInfo(String name) { //设置数据的重载方法
this.name=name;
}
public void showinfo() {
System.out.println("name="+name+"age="+age);
}
}
public class Bbb extends Aaa{
protected String college;
protected String institute;
public static void main(String[] args) {
Bbb s1=new Bbb();
s1.setInfo("tfnew21",18,"61272564654"); //调用父类的成员方法
s1.showinfo();
s1.college="计算机"; //访问本类的成员变量
s1.institute="计算机系"; //访问本类的成员变量
s1.showinfo();
System.out.print(s1.name+","+s1.college+","+s1.institute);
}
void setInfo(String name) {
this.college=name;
}
}