----------------------Java面向对象特性
1,对象的基本概念:面向对象程序语言三个关键特点:
封装 ( Encapsulation )
继承 ( Inheritance )
多态 ( Polymorphism )
在面向对象语言中,在数据类型的声明与操作这些数据的代码声明之间建立紧密联系,这种
联系通常描述为一种抽象数据类型。
在Java中,抽象数据类型的概念用类来实现。
1,2类和对象
1,2,1类是特定类型对象的定义,包括:对象包含的
数据,对象的创建及对象对自身数据操作的方法。
1,2,1类是一个模板。
1,3,1对象是在其类模型基础上构造出的,是具体的
实例。
类定义:Class EmpInfo{
String name;
String designation;
String department;
void print( ){ System.out.println(name+“is”+
designation+“at”+department);}
}
2,对象生命周期
创建对象
使用对象
清除不用的对象
3,创建对象
例:
Point origin_one ;
origin_one = new Point(23, 94);
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
对象实例化过程:构造与初始化对象的过程(调用new Xxxx()):
开辟内存空间(变量、方法体代码)及类成员 变量的初始化:数值型:0; 布尔型:false; 引用型:null;
字符串型:null;
显式初始化:执行类成员声明时带有的简单 赋值表达式。
public class Initialized{
private int x = 5 ;
private String name = “Fred”;
…
}
执行构造方法。
垃圾回收:
垃圾收集机制(garbage collection):Java运行环境当确定
某个对象不再被使用时,将其删除。
4,类
一般格式:
<modifiers><return_type><name>
([<argument_list>])[throws <exception>]{<block>}
方法是传值的,方法调用不会改变参数的值。当对象作为参数时,参数的值是该对象的引用,
这时对象的内容可以在方法中改变,但是对象的
引用不会改变。
例子:
Public class PassTest{
float ptValue;
public void changeInt(int value){
value = 55 ; }
public void changeStr(String value){
value = new String(“different”); }
public void changeObjValue( PassTest ref){
ref.ptValue = 99.0f; }
public static void main(String args[ ] ){
String str;
int val;
PassTest pt= new PassTest( );
val = 11;
pt.changeInt(val);
System.out.println(“Int value is:” +val);
str = new String(“hello”);
pt.changeStr(str);
System.out.println(“str value is:” +str);
pt.ptvalue = 101.0f ;
pt.changeObjValue(pt);
System.out.println(“pt value is:” +pt.ptValue);
}
}
关键字this 用来指向当前对象本身。
例:class Date {
private int day, month,year;
public Date getTommorrow( ){
this.day++;
…
}
5,重载
含义:在同一个类中一个方法名被用来定义多个方法。
重载必须遵守原则:
参数表必须不同,以此区分不同方法体。返回类型、修饰符可相同或不相同。
在一个构造方法中可以利用另一个构造方法。
class Employee{
private String name;
private int salary;
public Employee(String n, int s){
name = n ;
salary = s;
}
public Employee( String n){ this(n,0); }
public Employee( ){ this(“Unknown”); }
}
Java中用 extends 关键字定义子类。
public class Employee {
String name ;
Date hireDate ;
Date dateofBirth ;
String jobTitle ;
int grade ;
…
}public class Manager extends Employee {
String department ;
Employee [ ] subordinates;
}
子类
子类继承父类的属性、功能(方法),子类中只
需声明特有的东西。带private 修饰符的属性、方法是不能被继承的。
构造函数不能被继承。
在方法中调用构造方法用this()。调用父类的构造方法用super()。
-- super 指向该关键字所在类的父类
重写
子类可以改变从父类继承的行为。
被重写方法的返回值、方法名、参数列表要与父类中的方法完全一样。
public class Stack
{
private Vector items;
// code for Stack's methods and constructor not shown
// overrides Object's toString method
public String toString() {
int n = items.size();
StringBuffer result = new StringBuffer();
result.append("[");
for (int i = 0; i < n; i++) {
result.append(items.elementAt(i).toString());
if (i < n-1) result.append(",");
}
result.append("]");
return result.toString();
}
}
Super指向该关键字所在类的父类。
Public class Empolyee {
private String name ;
private int salary;
public String getDetails( ){
return “Name: ”+name+“\nSalary:”+salary;
}
}
public class Manager extends Empolyee {
private String department ;
public String getDetails( ){
return super.getDetailes( )+‘\nDepartment: “+
department;
}
}