面向对象程序设计的概念主要包括:对象,类,继承,动态绑定,多态性等特点.
1.对象:
可以对其在做事情的一些东西,一个对象有状态,行为和标识三种属性.
2.类:
是一种用户定义的引用数据类型,也称类类型.
在类的基础上还有包这一概念,包是组织类的一种方式,使用包的主要目的是保证类的唯一性.
可以使用import导入包中的类:
import java.util.Date;
public class Test{
public static void main(String[] args){
Date date = new Date();
System.out.println(date.getTime());
}
}
用import java.util.*;可以导入util包中的所有类,但这样容易出现冲突的情况,如:
import java.util.*;
import java.sql.*;
public class Test{
public static void main(string[] args){
Date date = new Date();
System.out.println(date.getTime());
}
}
//此时会编译出错
java.util包和java.sql包中都有Date类,所以对Date类的引用不明确.
使用import static可以导入包中的静态方法和字段.
import java.lang.System.*;
public class Test{
public static void main(String[] args){
out.println("hello world");
}
}
包的访问控制权限:
public:public可以修饰类,数据成员,方法和方法的成员.被public修饰的成员,可以在任何一个类中访问;
protected:被protected修饰的成员只能在定义的类和同一个包中的类访问,不同包中的子类也可以访问;
default:被default修饰的成员只能在定义的类和同一个包中的类中访问;
private:被private修饰的成员只能在定义的类中访问.
3.继承:
语法格式为:
class 子类 extends 父类{
}
1)当子类和父类位于同一个包中时,子类继承父类中public,protected,和默认访问级别的成员变量和成员方法.
2)当字类和父类位于不同的包中时,子类继承父类中public和protected访问级别的成员变量和成员方法.
fianl关键字:
final int a = 10;
//a表示常量
final public class Test{
}
//final修饰类,表示该类不能再被继承.
组合:组合和继承类似,也表示类之间的关系.
public class Teacher{
……
}
public class Student{
……
}
public class School{
public Teacher[] teacher;
public Student[] student;
}
多态:
向上转型:
即一个父类的引用指向子类的实例.
Bird bird = new Bird(); //bird是Bird类的一个实例
Animal animal = bird; //Animal是Bird的父类
向上转型这样的写法可以结合is - a语义来理解.
向上转型主要用于:
1)直接赋值;
2)方法传参;
3)方法返回.
方法传参:
public class Test{
public static void main(String[] args){
Bird bird = new bird("小鸟"); //实例化Bird类的对象
feed(bird);
}
public static void feed(Animal animal){ //feed方法
animal.eat("谷子"); //animal是bird父类的引用
}
}
方法返回:
public class Test{
public static void main(String[] args){
Animal animal = findTheAnimal();
}
public static Animal findTheAnimal(){
Bird bird = new Bird("小鸟"); //实例化Bird类的对象
return bird; //返回Bird实例的引用
}
}
4.动态绑定:
在java中,调用某个方法时,究竟执行的是父类还是子类的代码,要看这个引用指向的是父类还是子类的对象.这个过程是在程序运行时决定的,因此称为动态绑定.
public class Animal{
private String name;
public Animal(String name){
this.name = name;
}
public void eat(String food){
System.out.println("我是一只动物.");
System.out.println(this.name + "正在吃" + food + ".");
}
public class Bird{
public Bird(String name){
super(name);
}
public void eat(String food){
System.out.priintln("我是一只小鸟.");
System.out.println(this.name + "正在吃" + food + "." );
}
}
public class Test{
public static void main(String[] args){
Animal animal1 = new Animal("花花");
animal.eat("猫粮");
Animal animal2 = new Animal("小鸟儿");
animal.eat("谷子");
}
}
/*执行结果:
我是一只动物
花花正在吃猫粮
我是一只小鸟
小鸟儿正在吃谷子*/
向下转型:
即父类对象转为子类对象.
public class Animal{
private String name;
public Animal(String name){
this.name = name;
}
public void eat(String food){
System.out.println("我是一只动物.");
System.out.println(this.name + "正在吃" + food + ".");
}
public class Bird{
public Bird(String name){
super(name);
}
public void eat(String food){
System.out.priintln("我是一只小鸟.");
System.out.println(this.name + "正在吃" + food + "." );
}
public void fly(){
System.out.println(this.name + "正在飞");
}
}
public class Test{
Animal animal = new Animal("圆圆");
animal.eat("谷子"); // 输出 我是一只鸟 圆圆正在吃谷子
animal.fly(); //编译出错,找不到fly方法
//编译器检查有哪些方法存在,看的是Animal这个类型
//执行时究竟执行父类还是子类的方法,要看Bird这个类型
//正确操作如下:
if (animal instanceof Bird){
Bird bird = (Bird)animal;
bird.fly();
}
}
super关键字表示获取到父类实例的引用.
如上例中的:
public Bird(String name){
super(name);
}
表示通过super来调用父类的构造函数.除此之外还有:
super.eat("谷子"); //表示调用父类eat方法
5.抽象类:
抽象类就是没有具体实例对象的类,只有方法声明而没有方法实现的方法称为抽象方法,含有抽象方法的类就称为抽象类.在Java中,当一个类被声明为abstract时,这个类就是抽象类.
abstract是抽象修饰符,可以用来修饰类和方法.
抽象类也有一些需要注意的规则:
(1)抽象类不能直接实例化,并且对抽象类使用new运算符是编译时错误.
(2)抽象类的数据成员和成员方法都是其子类的公共数据成员和方法的集合.
(3)抽象类中既存在抽象方法,也存在一般方法.
(4)对于父类中的抽象方法是通过子类覆盖父类抽象方法的形式来实现继承的,子类必须实现父类的所有抽象方法,否则该子类必须定义为抽象类,即如果定义一个具体子类继承于某个抽象父类,那么该子类必须实现抽象父类中的抽象方法.
(5)抽象类不能被密封,即抽象类不能修饰为private,也不能喝final一起组合使用,因为抽象类本身就是不完整的,需要别的类去实现它不完整的部分.
abstract class PrintMechine{
abstract public void print(int x);
}
public class AbstractTest extends PrintMechine{
int a;
public AbstractTest(int a){
this.a = a;
}
public void print(int a){
System.out.println(a*2);
}
public static void main(String[] args){
PrintMechine p1 = new AbstractTest(3);
p1.print(3);
}
}
//输出结果:6
6.接口:
接口由常量和抽象方法组成的特殊类,接口中只能包含抽象方法和静态常量.
语法定义如下:
[public]interface 接口名[extends 父接口名列表]{
[public][static][final]域类型 域名=常量值;
[public][abstract][native]返回值 方法名(参数列表)[throw 异常列表];
}
public interface Student_info{
int year = 2008;
int age();
void outout();
}
(1)interface 是声明接口的关键字,可以把它看成一个特殊的类.
(2)声明接口可给出访问控制符,用public修饰的是公共接口,可以被所有的类和接口使用.
(3)接口名要求符合java标识符的规定.
(4)接口也具有继承性,定义一个接口时,可以通过extends关键字声明该接口是某个已经存在的父借口的派生接口,它将继承父接口的所有属性和方法.一个接口还可以继承多个父接口,父接口减用逗号分隔.
(5)系统默认接口中所有属性的修饰都是public static final.
(6)系统默认接口中所有方法的修饰都是public abstract.
(7)在接口中对抽象方法声明时,只能给出这些抽象方法的方法名,返回值和参数列表,而不能定义方法体,即这些接口仅仅是规定了一组信息交换,传输和处理的"接口".
实现接口:
声明格式:
[<修饰符>]class<类名>[extends<超类名>][implements<接口名1>,<接口名2>,……]
(1)一个类可以实现多个接口,用逗号分隔接口列表.
(2)如果实现某接口的类不是abstract的抽象类,则在类的定义部分必须实现指定接口的所有抽象方法,即为所有抽象方法定义方法体,而且方法头部分应该与接口中的定义完全一致,即有完全相同的返回值和参数列表.
(3)如果实现某接口的类是abstract的抽象类,则它可以不实现该接口所有的方法.但是对于这个抽象类的任何一个非抽象的子类而言,它们的父类所实现的接口中的所有抽象方法都必须有实在的方法体.
(4)接口的抽象方法 的访问限制符都已指定为public,一个类在实现一个接口时,必须实现接口中的所有方法,并且方法必须声明为public.
public class Student implements Student_info{ //Student_info已在前面定义
String name;
String sex;
int birth_year;
public Student(String n, String s,int y){ //Student的构造函数
name = n;
sex = s;
birth_year = y;
}
public int age(){
return year - birth_year;
}
public void output(){
System,out.println(this.name + " " +this.sex+ " " +this.age() + "岁");
}
public static void main(String[] args){
Student stu = new Student("小红","女",1983);
stu.output();
}
}
运行结果:
小红 女 25岁
实现多个接口:
interface theShape{
double PI = 3.14159;
double getArea();
}
interface showShape{
void showInfor();
}
public class Circle implement theShape,showShape{
int r;
Circle(int r){
this.r = r;
}
public double getArea(){
return r*r*PI;
}
public void showInfor(){
System.out.print("r=" + r +"\t the area: "+ getArea());
}
public static void main(String[] args){
Circle c = new Circle(10);
c.showInfor();
}
}
//运行结果:
r=10 the area: 314.159
抽象类和接口的区别:
在组织结果上:
抽象类:有普通方法和抽象方法;
接口:抽象方法和全局常量;
在权限上:
抽象类:有各种权限;
接口:public;
在子类使用:
抽象类:使用extends关键字继承抽象类;
接口:使用implements关键字实现接口;
关系:
抽象类:一个抽象类可以实现若干个接口;
接口:接口不能继承抽象类,但是接口可以使用extends关键字集成多个父接口;
子类限制:
抽象类:一个子类只能继承一个抽象类;
接口:一个子类可以实现多个接口.
1944

被折叠的 条评论
为什么被折叠?



