6.1 类和对象概述
6.1.1 面向对象的基本概念
1.类: 类是对对象的抽象描述。
2.对象: 对象是表示现实世界中某个具体的事物;
6.1.2 类的声明
[修饰符] class 类名 [extends 父类名] [implements 接口名列表]
{
类成员变量声明;
类方法声明;
}
例:定义一个表示二维平面上点的类 class Point { private int x,y; public void setPoint(int a,int b) { x=a; y=b; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "["+x+","+y+"]"; } }
6.1.3 对象的创建和使用
[修饰符] 类名 对象名=new 类名(实参列表);
或[修饰符] 类名 对象名;
对象名=new 类名(实参列表);
用户需要应用new完成分配空间的任务。
thePoint=new Point( );
对象的使用
引用成员变量 对象名.成员变量名
例:定义一个表示圆形的类,能够计算圆面积和周长。 class Circle1 { float r; final double PI=3.14159; public double area() //计算面积 { return PI*r*r; } public void setR(float x) //设置半径 { r=x; } public double perimeter() //计算周长 { return 2*PI*r; } public static void main(String args[]) { double x,y; Circle1 cir=new Circle1(); //创建Circle1类的对象cir cir.setR(12.35f); //引用cir对象的setR()方法 x=cir.area(); //引用cir对象的area()方法 y=cir.perimeter(); //引用cir对象的perimeter()方法 System.out.println("圆面积="+x+"\n圆周长="+y); } }
程序运行结果如下:
圆面积=479.163190376011
圆周长=77.59727539684296
6.1.4 构造方法和对象的初始化。
构造方法的特点
构造方法名与类名相同;
构造方法没有返回值;
构造方法的主要作用是对对象初始化。
class Triangle { int x,y,z; public Triangle(int i,int j,int k) //声明构造方法 { x=i; y=j; z=k; } public static boolean judge(Triangle m) { if(Math.sqrt(m.x*m.x+ m.y*m.y)== Math.sqrt(m.z*m.z)) //引用Math类库的sqrt()方法 return true; else return false; } public static void main(String args[]){ Triangle t1; //声明Triangle类对象t1 t1=new Triangle(3,4,5); //实例化对象t1,调用构造方法对其进行初始化 if(judge(t1)) //调用judge()方法,判断t1的成员变量是 //否能构成直角三角型的3个边长 System.out.println("这是一个直角三角形"); else System.out.println("这不是一个直角三角形"); } }
例6-4 默认构造方法的使用
class Student { String name; String address; int grade; Student(String x1,String x2,String x3,int y) { //定义构造方法 name=x1; address=x2; grade=x3; } public static void main(String args[]) { Student1 zhang; //声明并创建zhang对象 zhang=new Student("张三","西安市兴庆路1号",3); Student1 wang; //声明并创建wang对象 wang=new Student("王五","西安市翠华路12号",4); System.out.println(zhang.name+zhang.address+zhang.grade); System.out.println(wang.name+wang.address+wang.grade); } }
缺省构造方法的使用
class Student { String name; //成员变量 String address; //成员变量 int score; //成员变量 public void setMessage(String x1,String x2, int x3) //成员方法 { name=x1; address=x2; score=x3; } public static void main(String args[]) { Student s1=new Student(); //创建Student类对象s1 System.out.println(s1.name+" "+s1.address+" "+s1.score); //输出缺省构造方法的初始化结果 s1.setMessage("张三","西安市兴庆路1号",75); // 调用成员方法给成员变量赋值 System.out.println(s1.name+" "+s1.address+" "+s1.score); } }
程序运行结果如下:
null null 0
张三 西安市兴庆路1号 75
6-5 使用无参数的构造方法
class Time { private int hour; //0-23 private int minute; //0-59 private int second; //0-59 public Time() { setTime(0,0,0); } public void setTime(int hh, int mm, int ss) { hour = ((hh >= 0 && hh <24) ? hh : 0); minute = ((mm >= 0 && mm < 60) ? mm : 0); second = ((ss >= 0 && ss < 60) ? ss : 0); } public String toString() { return (hour + ":" + (minute < 10 ? "0" : "") + minute + ":" + (second < 10 ? "0" : "") + second ); } } public class MyTime { public static void main(String args[]) { Time time=new Time(); time.setTime(11,22,33); System.out.println(" set time =" + time.toString()); } }
运行结果如下:
set time =11:22:33
例6-6 使用多个构造方法
class Time1 { private int hour; //0-23 private int minute; //0-59 private int second; //0-59 public Time1() { setTime (0,0,0); } public Time1(int hh) { setTime (hh,0,0); } public Time1 (int hh, int mm) { setTime (hh,mm,0); } public Time1(int hh, int mm, int ss) { setTime (hh,mm,ss); } public void setTime (int hh, int mm, int ss) { hour = ((hh >= 0 && hh < 24) ? hh : 0); minute = ((mm >= 0 && mm < 60) ? mm : 0); second = ((ss >= 0 && ss < 60) ? ss : 0);} public String toString() { return (hour + ":" +(minute < 10 ? "0" : "") + minute + ":" + (second < 10 ? "0" : "") + second); } } public class MyTime1 { private static Time1 t0, t1, t2, t3; public static void main(String args[]) { t0=new Time1(); t1=new Time1(11); t2=new Time1(22, 22); t3=new Time1(33, 33, 33); System.out.println(" t0= " + t0.toString()); System.out.println("t1= " + t1.toString()); System.out.println("t2= " + t2.toString()); System.out.println(“t3= ” + t3.toString()); } }
程序运行结果如下:
t0= 0:00:00
t1= 11:00:00
t2= 22:22:00
t3= 0:33:33
6.1.5 对象销毁
通过new运算符实例化对象时,系统为对象分配所需的存储空间,存放其属性值。
但内存空间有限,不能存放无限多的对象。为此,Java提供了资源回收机制,自动销毁
无用对象,回收其所占用的存储空间。如果需要主动释放对象,或在释放对象时需要执
行特定操作,则在类中可以定义finalize()方法
public void finalize() { 方法体; }
6.2 类的封装
封装性是面向对象的核心特征之一。类的封装包含2层含义:将数据和对数据的操作组合起来构成类,
类是一个不可分割的独立单位;类中既要提供与外部联系的接口,同时又要尽可能隐藏类的实现细节
6.2.1 访问权限
private(私有):被private修饰的成员变量和成员方法只能在声明他们的类中访问,而不能
在其他类(包括其子类)中访问
访问控制 | 本类 | 同一包 中的类 | 其他包 中子类 | 其他包 中的类 |
public | Ö | Ö | Ö | Ö |
private | Ö | × | × | × |
protected | Ö | Ö | Ö | × |
缺省 | Ö | Ö | × | × |
例6-7 权限修饰符的作用。
class Time { private int hour; //0-23 private int minute; //0-59 private int second; //0-59 public Time() { setTime(0,0,0); } public void setTime(int hh, int mm, int ss) { hour = ((hh >= 0 && hh <24) ? hh : 0); minute = ((mm >= 0 && mm < 60) ? mm : 0); second = ((ss >= 0 && ss < 60) ? ss : 0); } public String toString() { return (hour + ":" + (minute < 10 ? "0" : "") + minute + ":" + (second < 10 ? "0" : "") + second ); } } public class MyTime2 { public static void main(String args[]) { Time time = new Time(); time.hour = 11; //欲将11赋给hour成员变量,但属于非法访问 System.out.println("time" + time.toString()); } }
类的访问权限
声明一个类可使用的权限修饰符只有public和缺省2种 。
虽然一个Java源程序文件中可以包含多个类,但只能有一个类使用public修饰符,
的名字与源程序文件的名字相同。 当程序中创建多个类时,必须运行包含main()方法的类,
否则,出错 。
6.2.2 类成员
1.类成员变量
(1) 在类中声明成员变量时,没有使用static修饰的变量为实例成员变量,使用static修饰的变量为类成员变量
class Student { String name; //实例成员变量 String sex; //实例成员变量 static int count=0; //类成员变量 public Student(String m, String s ) { name=m; sex=s; count=count+1; } }
(2).当创建对象时,每个对象拥有各自的实例成员变量,各对象的实例成员变量具有不同的值;
而系统仅为类成员变量分配一个存储单元,所有对象共享一个类成员变量。
(3) 实例成员变量属于对象,只能通过对象引用;类成员变量属于类,既可以通过类名访问
,也可以通过对象名访问。 实例成员变量的访问方法为:
对象名. 实例成员变量名
类成员变量的访问方法为:
对象名.实例成员变量名 或 类名.实例成员变量名
例:6-8
类成员变量和实例成员变量的对比 class Student1 { String name; //实例成员变量 String address; //实例成员变量 static int count=0; //类成员变量 public Student1(String m, String a ) { name=m; address=a; count=count+1; } public static void main(String args[]) { Student1 p1=new Student1("李明","西安市未央区"); Student1 p2=new Student1("张敏", "上海市闽行区"); System.out.println(p1.name+" "+p1.address+" "+p1.count); Student1.count=Student1.count+1; System.out.println(p2.name+" "+p2.address+" "+p2.count); p1.count=p1.count-1; System.out.println(p2.name+" "+p2.address+" "+p2.count); } }
程序运行结果如下:
李明西安市未央区 2
张敏上海市闽行区 3
张敏上海市闽行区 2
2.类成员方法
在类中声明成员方法时,没有使用static修饰的方法为实例成员方法,使用static修饰的方法为 类成员方法。
public static double area(double r)
{
return 3.14*r*r;
}
类成员方法中除使用本方法中声明的局部变量外,只可以访问类成员变量,不能访问实例成
员变量;实例成员方法中除使用本方法中声明的局部变量外,还可以访问类成员变量及实例成员变量
例6-9
例:类成员方法和实例成员方法的对比 class Course { String no; //实例成员变量:课程编号 int score; //实例成员变量:成绩 static int sum=0; //类成员变量:总成绩 public Course(String n, int s) { no=n; score=s; } public static void summarize(int s) //类方法:统计总成绩 { sum+=s; } } public class Statistic { public static void main(String args[]) { Course c1,c2; c1=new Course("210",90); Course.summarize(90); System.out.println("sum="+c1.sum); c2=new Course("300",80); c2.summarize(80); System.out.println("sum="+Course.sum); } }
程序运行结果如下:
sum=90
sum=170
3 数学函数类—— Math类库
Java类库中的Math类提供了很多常用数学函数的实现方法,这些方法
都是static方法,通过类名Math调用,其调用方式如下:
Math.方法名
Math类中的常用方法包括:
sin(double x)
cos(double x)
log(double x) //返回x的自然对数
exp(double x) //返回ex
abs(double x) //返回x的绝对值
max(double x, double y) //返回x和y中的较大值
sqrt(double x) //返回x的平方根
random(double x) //返回[0, 1]区间内的随机数
pow(double y, double x) //返回yx
例6-10 输入两个数.输出其中较大者
public class Max { public static void main(String args[]) { int x,y; x=Integer.parseInt(args[0]); y= Integer.parseInt(args[1]); System.out.println("最大值是"+Math.max(x,y)); } }