对于类与对象需要进行一个基本的介绍:
对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。
类:类是一个模板,它描述一类对象的行为和状态。
那么,我们又应该怎么样去了解对象呢?下面我们具体点的讲解关于这块的内容:
现在让我们深入了解什么是对象。看看周围真实的世界,会发现身边有很多对象,车,狗,人等等。所有这些对象都有自己的状态和行为。
拿一条狗来举例,它的状态有:名字、品种、颜色,行为有:叫、摇尾巴和跑。
对比现实对象和软件对象,它们之间十分相似。
软件对象也有状态和行为。软件对象的状态就是属性,行为通过方法体现。
在软件开发中,方法操作对象内部状态的改变,对象的相互调用也是通过方法来完成。
而类则是:
类可以看成是创建Java对象的模板。
一个类可以包含以下类型变量:
局部变量:在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中,方法结束后,变量就会自动销毁。
成员变量:成员变量是定义在类中,方法体之外的变量。这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问。
类变量:类变量也声明在类中,方法体之外,但必须声明为static类型。
下面,我将通过三个例子对于类与对象的基本概念进行基本的阐述:
* 1、*声明Patient类表示门诊室中的病人,此类对象应包括name(a string)、sex(char)、age(an integer)、weight(a float)、allergies(a boolean)。声明存取及修改方法。在一个单独的类中,声明测试方法,并生成两个patient对象,设置其状态并将其信息显示在屏幕上。
package Lei;
class Patient{
private String name; //姓名
private char sex; //性别
private int age; //年龄
private float weight; //体重
private boolean allergies; //过敏
public Patient() { //无参
super();
}
public Patient(String name, char sex, int age, float weight, boolean allergies) {//实参构造方法
super();
this.name = name;
this.sex = sex;
this.age = age;
this.weight = weight;
this.allergies = allergies;
}
public String getName() { //获取姓名
return name;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public char getSex() { //获取性别
return sex;
}
public void setSex(char sex) { //设置性别
this.sex = sex;
}
public int getAge() { //获取年龄
return age;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public float getWeight() { //获取体重
return weight;
}
public void setWeight(float weight) { //设置体重
this.weight = weight;
}
public boolean isAllergies() { //获取是否为过敏
return allergies;
}
public void setAllergies(boolean allergies) { //判断是否过敏
this.allergies = allergies;
}
public String toString() { //使用toString,将进行覆写
return "Patient [name=" + this.name + ", sex=" + this.sex + ", age=" + this.age + ", weight=" + this.weight + ", allergies="
+ this.allergies + "]";
}
public void getInfo(){
System.out.println("姓名:" + this.getName() + ",性别:" + this.getSex()
+ ",年龄:" + this.getAge() + ",体重:" + this.getWeight()
+ ",过敏情况:" + this.isAllergies() );}
}
public class test1 {
public static void main(String args[]) {
//需要生成两个Patient量
Patient p1 = new Patient();
Patient p2 = new Patient();
System.out.println("================下面使用set、get方法==================");
p1.setName("张三");
p1.setSex('女');
p1.setAge(23);
p1.setWeight((float) 110.6);
p1.setAllergies(false);
p2.setName("李四");
p2.setSex('男');
p2.setAge(40);
p2.setWeight((float) 190.8);
p2.setAllergies(false);
p1.getInfo();
p2.getInfo();
System.out.println("================下面使用toString方法一==================");
Patient p3 = new Patient("王五",'女',23,110,false);
Patient p4 = new Patient("张六",'男',32,223,true);
System.out.println(p3);
System.out.println(p4);
}
}
2、声明并测试toString()方法显示一个病人的age、sex、name及allergies属性。
package Lei;
class Patienter{
private String name; //姓名
private char sex; //性别
private int age; //年龄
private float weight; //体重
private boolean allergies; //过敏
public Patienter() { //无参
super();
public Patienter(String name, char sex, int age, float weight, boolean allergies) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.weight = weight;
this.allergies = allergies;
}
public Patienter(String name, char sex, int age, boolean allergies) {//实参构造方
super();
this.name = name;
this.sex = sex;
this.age = age;
this.allergies = allergies;
}
public String getName() { //获取姓名
return name;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public char getSex() { //获取性别
return sex;
}
public void setSex(char sex) { //设置性别
this.sex = sex;
}
public int getAge() { //获取年龄
return age;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public float getWeight() { //获取体重
return weight;
}
public void setWeight(float weight) { //设置体重
this.weight = weight;
}
public boolean isAllergies() { //获取是否为过敏
return allergies;
}
public void setAllergies(boolean allergies) { //判断是否过敏
this.allergies = allergies;
}
public String toString() { //使用toString,将进行覆写
return "Patient [姓名=" + this.name + ", 性别=" + this.sex + ", 年龄=" + this.age + ", 过敏情况="+ this.allergies + "]";
}}
public class Test2 {
public static void main(String args[]) {
//需要生成两个Patient量
System.out.println("================下面使用toString方法一==================");
Patienter p1 = new Patienter("王五",'女',23,false);
Patienter p2 = new Patienter("张六",'男',32,true);
System.out.println(p1);
System.out.println(p2);
}}
3、利用可变长参数编写一个函数,求一个圆和若干个矩形对象中的最大面积。
//利用可变长参数编写一个函数,求一个圆和若干个矩形对象中的最大面积
class Circle{
static double PI = 3.1415;
int radius;
public double area(){
return PI * radius * radius;
}}
class Rectangle{
double width;
double height;
public double area(){
return width * height;
}}
public class VarArg {
public static void main(String[] args){
Circle c = new Circle();
c.radius = 10;
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
r1.width = 20;
r1.height = 30;
r2.width = 10;
r2.height = 40;
System.out.println(maxArea(c,r1,r2));
System.out.println(maxArea(c,r1));
System.out.println(maxArea(c,r2));
System.out.println(maxArea(c));
}
static double maxArea(Circle c,Rectangle...varRec){
double max = 0.0; //将最大数值设置为0.0
max = c.area(); //将实例化c中的面积直接赋值给max
Rectangle[] rec = varRec;
for (Rectangle r:rec){
if (r.area() > max){
max = r.area();
}
}
return max;
}}