04747_Java语言程序设计(一)_第3章_面向对象编程基础

链式编程

每次调用方法后,返回的是一个对象

/*
* 链式编程
* 每次调用方法后,返回的是一个对象
*/ class Student {
public void study() {
System.out.println("good good study, day day up!");
}
} class StudentDemo {
public Student getStudent() {
return new Student();
}
} public class InnerClassDemo { public static void main(String[] args) {
// TODO Auto-generated method stub StudentDemo sd = new StudentDemo(); sd.getStudent().study();// 链式编程
}
}

面试题:

要求分别输出30,20,10

注意:

1:内部类和外部类没有继承关系

2:通过外部类名限定this对象

3:Outer.this

/*
面试题:
要求分别输出30,20,10 注意:
1:内部类和外部类没有继承关系
2:通过外部类名限定this对象
3:Outer.this
*/ class Outer {
public int num = 10; class Inner {
public int num = 20; public void show() {
int num = 30;
System.out.println(num);
System.out.println(this.num);
System.out.println(Outer.this.num);
}
}
} public class InnerClassDemo { public static void main(String[] args) {
// TODO Auto-generated method stub Outer.Inner oi = new Outer().new Inner();
oi.show();
}
}

匿名内部类在开发中的使用

/*
* 匿名内部类在开发中的使用
*/ interface Person {
public abstract void study();
} class PersonDemo {
// 接口名作为形式参数
// 起始这里需要的不是接口,而是该接口的实现类
public void method(Person p) {
p.study();
}
} class Student implements Person// 实现类
{
public void study() {
System.out.println("good good study, day day up!");
}
} public class InnerClassDemo { public static void main(String[] args) {
// TODO Auto-generated method stub PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p); System.out.println("-----------------------------"); pd.method(// 上下作用一样,下面的在Android更常用,因为匿名对象使用以后内存被回收
new Person() {
public void study() {
System.out.println("good good study, day day up!");
}
});
}
}

匿名内部类的面试题

class Outer {
public static Inter method() {
return new Inter() {
public void show() {
System.out.println("hello world.");
}
};
}
} public class InnerClassDemo { public static void main(String[] args) {
// TODO Auto-generated method stub Outer.method().show();
/*
* 1: Outer.method()可以看出method()应该是Outer的一个静态方法
* 2:Outer.method().show()可以看出method()方法的返回值是一个对象
* 3:接口Inter中有一个show()方法,method()方法的返回值是一个接口
*/
}
}

面试题

/*
* 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲区获取数据
* */

public class IntegerDemo {

	public static void main(String[] args) {
// TODO Auto-generated method stub Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2); Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4); Integer i5 = 127;
Integer i6 = 127;
System.out.println(i5 == i6); Integer i7 = 128;
Integer i8 = 128;
System.out.println(i7 == i8); /*
* 通过查看源码,得知针对-128到127之间的数据,做了一个数据缓冲区,如果数据在该范围内的,每次并不创建新的空间 以下是反编译后的语句
* Integer i9 = Integer.valueOf(128);
*/ Integer i9 = Integer.valueOf(128);
Integer i10 = Integer.valueOf(128);
System.out.println(i9 == i10);
}
}

例3.1学生类Student的声明。

class Student {
float heigth, weight;// 成员变量定义
String name, sex, no;// 成员变量定义 void setStudent(String n, String s, String o)// 方法定义
{
name = n;
sex = s;
no = n;
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("学号:" + no);
} void setWH(float w, float h)// 方法定义
{
weight = w;
heigth = h;
}
}

例3.2方法的例子,类B有方法u()和v()。

class B {
double x, y; int u(int x, int y)// 方法u()返回int型值,有两个int型参数
{
return x * x + y * y + 1;// x、y是参数
} float v(int a, float b) {
return (float) (a * x + b * y);// x、y是成员变量
}
}

例3.3利用关键字this访问被隐藏的成员变量。

class Jerry {
int x, z; void g(int z) {
int x = 5 + z;// 定义局部变量x,而z是参数
this.x = x;// 局部变量x的值赋值给成员变量x
this.z = z;// 参数z的值赋值给成员变量z
}
}

例3.4方法重载的例子,类C的4个方法或因参数个数不同,或因参数的类型顺序不同,是4个合理的重载方法。

class C {
float fun(float s)// 只有一个float参数
{
return s * s;
} float fun(float x, int y)// 第一个是float参数,第二个int参数
{
return x * x + y * y;
} float fun(int x, float y)// 第一个是int参数,第二个float参数
{
return x * x + y * y;
} float fun(float x, float y, float z)// 有三个是float参数
{
return x * x + y * y + z;
}
}

例3.5类内定义构造方法的例子。类Point定义了两个构造方法。

class Point {
int x, y; Point()// 无参数的构造方法,点对象被创建时,总是预设x=10,y=20
{
x = 10;
y = 20;
} Point(int x, int y)// 有两个参数的构造方法,为点对象设置x和y坐标的初值
{
this.x = x;// 成员变量与参数同名,冠this是指成员变量,参见3.2.7
this.y = y;
} int getX() {
return x;
} int getY() {
return y;
}
}

例3.6使用对象的程序例子。

public class Example3_1 {
public static void main(String[] args) {
Point p1, p2, p3;// 声明对象p1,p2,p3
p1 = new Point();// 创建对象p1
p2 = new Point(40, 50);// 创建对象p2
p3 = new Point(p1.getX() + p2.getX(), p1.getY() + p2.getY());
System.out.println("P3.x=" + p3.getX() + ",p3.y=" + p3.getY());
Point p4 = new Point(p1.x, p2.y);// 声明并创建对象p4
System.out.println("P4.x=" + p4.x + ",p4.y=" + p4.y);
}
}

例3.7类D定义了一个实例方法,两个类方法。

class D {
int a;// 实例变量
static int c;// 类变量 float max(int x, int y)// 实例方法
{
return a = x > y ? x : y;
} static void setCount(int c0)// 类方法
{
c = c0;
} static void incCount(int step)// 类方法
{
c += step;
}
}

例3.8含不合法代码的例子。程序的注释指明合法和不合法的原有。

class E {
float u;
static float v; static void setUV(boolean f) {
u = s_m(f);// 合法,类方法可以调用类方法
v = r_m(!f);// 非法,类方法不能直接调用实例方法
} static float s_m(boolean f) {
return f ? u : v;// 非法,类方法只能引用类变量
} float r_m(boolean f) {
return f ? u : v;// 合法,实例方法能引用实例变量和类变量
}
}

例3.9说明类变量用法的应用程序。改写Point类的声明,在Point类中增加一个类变量pCount,它的初值为0。在构造方法中,有类变量pCount增1的代码,这能记录类的对象个数。

public class Example3_2 {
public static void main(String[] args) {
Point p1, p2, p3;
p1 = new Point();
p2 = new Point(40, 50);
p3 = new Point(p1.getX() + p2.getX(), p1.getY() + p2.getY());
System.out.println("p3.x=" + p3.getX() + ",p3.y=" + p3.getY());
Point p4 = new Point(p1.x, p2.y);
System.out.println("p4.x=" + p4.x + ",p4.y=" + p4.y);
System.out.println("程序共有Point对象" + Point.pointNum() + "个");
}
} class Point {
int x, y;
static int pCount = 0; Point()// 无参数的构造方法,点对象被创建时,总是预设x=10,y=20
{
x = 10;
y = 20;
pCount++;
} Point(int x, int y)// 有两个参数的构造方法,为点对象设置坐标的初值
{
this.x = x;// 由于成员变量与参数同名,this表明是成员变量
this.y = y;
pCount++;
} static int pointNum() {
return pCount;
} int getX() {
return x;
} int getY() {
return y;
}
}

例3.10继承声明子类的例子。

class Mother {
private int money;
float weight, height; String speak(String s) {
return s;
} float getWeight() {
return weight;
} float getHeight() {
return height;
} String dance() {
return "我会跳舞";
}
} class Daughter extends Mother {
String cat; String sing(String s) {
return s;
} String dance() {
return "我是小舞蹈演员";
}
} public class Example3_10 {
public static void main(String[] args) {
Daughter girl = new Daughter();
girl.cat = "漂亮的帽子";
girl.weight = 35.0f;
girl.height = 120.0f;
System.out.println(girl.speak("我是女儿"));
System.out.println(girl.speak("我像母亲一样很会说话"));
System.out.println(girl.speak("我重" + girl.weight + "公斤"));
System.out.println(girl.speak("我高" + girl.height + "公分"));
System.out.println(girl.speak("我还比母亲多一项" + girl.cat));
System.out.println(girl.sing("我还能唱歌"));
System.out.println(girl.dance());
}
}

例3.11说明多态性的程序例子。

class Shape {
float area()// 抽象的几何形状,一律返回面积为0
{
return 0.0f;
}
} class Circle extends Shape {
float R; Circle(float r) {
R = r;
} float area()// 圆类重新定义求面积的方法,能正确求出圆的面积
{
return 3.1415926f * R * R;
}
} class Rectangle extends Shape {
float W, H; Rectangle(float w, float h) {
W = w;
H = h;
} float area()// 长方形类也重新定义求面积的方法,能正确求出长方形的面积
{
return W * H;
}
} public class Example3_4 {
public static void main(String[] args) {
Circle c;
Rectangle r;
c = new Circle(1.0f);
r = new Rectangle(3.0f, 4.0f);
System.out.println("圆面积=" + c.area());
System.out.println("长方形面积=" + r.area());
;
}
}

例3.12一个多层继承的例子。

class Vehicle {
public void start() {
System.out.println(" Starting...");
}
} class Aircraft extends Vehicle {
public void fly() {
System.out.println(" Flying...");
}
} class Whirlybird extends Aircraft {
public void whirl() {
System.out.println(" Whirling...");
}
} class Jet extends Aircraft {
public void zoom() {
System.out.println(" Zooming...");
}
}

例3.13说明多层继承中构造方法调用顺序的例子。

class A {
int a; A(int a) {
this.a = a;
System.out.println("Constructing A");
}
} class B extends A {
int b; B(int a, int b) {
super(a);
this.b = b;
System.out.println("Constructing B");
}
} class C extends B {
int c; C(int a, int b, int c) {
super(a, b);
this.c = c;
System.out.println("Constructing C");
}
} class D extends C {
int d; D(int a, int b, int c, int d) {
super(a, b, c);
this.d = d;
System.out.println("Constructing D");
}
} public class Example3_13 {
public static void main(String[] args) {
D obj = new D(1, 2, 3, 4);
System.out.println("对象obj的值是:" + obj.a + "," + obj.b + "," + obj.c + "," + obj.d);
System.out.println("Main Program!");
}
}

例3.14含抽象类和抽象方法的程序。

abstract class Shape// 抽象类
{
int x, y; void MoveTo(int newx, int newy) {
x = newx;
y = newy;
} Shape(int newx, int newy) {
x = newx;
y = newy;
} abstract void Draw();// 抽象方法,没有实现
} class Square extends Shape// 继承抽象类
{
int len;// 正方形边长 Square(int px, int py, int l) {
super(px, py);
len = l;
} void Draw()// 超类抽象方法的具体的实现
{
System.out.print("我是正方形,");
System.out.print("我的中心位置是:" + "(" + x + "," + y + "),");
System.out.println("我的边长是:" + len);
// 以x,y为中心,边长为len的正方形
}
} class Circle extends Shape// 继承抽象类
{
int radius;// 半径 Circle(int px, int py, int r) {
super(px, py);
radius = r;
} void Draw()// 超类抽象方法的具体的实现
{
System.out.print("我是圆形,");
System.out.print("我的中心位置是:" + "(" + x + "," + y + "),");
System.out.println("我的半径是:" + radius);
// 以x,y为圆心,半径为radius的圆
}
} class ShapeDraw {
void draw(Shape obj) {
obj.Draw();// 利用动态联编,按实际对象调用相应的Draw()方法
}
} public class Example3_14 {
public static void main(String[] args) {
ShapeDraw sd = new ShapeDraw();
Square s = new Square(10, 10, 20);
Circle c = new Circle(30, 30, 5);
sd.draw(s);// 以s为实参调用sd的draw方法
sd.draw(c);// 以c为实参调用sd的draw方法
}
}

例3.15使用super调用超类构造方法、超类方法和超类成员变量的程序。

class A {
int x; A(int inf) {
x = inf;
} void method() {
System.out.println("我是类A的方法!值是" + x);
} int getX() {
return x;
}
} class B extends A// 继承类A声明类B
{
double x; B(int a, double b) {
super(a);// 调用类A的构造方法
x = b;
} void method() {
System.out.println("我是类B的方法!值是" + x);
}
} class C extends B {
char x; C(int a, double b, char c) {
super(a, b);// 调用类B的构造方法
x = c;
} void method() {
char chCx = x;// 这里的x是类C定义的x
double dBx = super.x;// 引用父类的成员变量,类B的x
int iAx = this.getX();// 不能用A.X,也不能用super.super.x
super.method();// 调用类B的method()方法
System.out.println("我是类C的方法!值是" + x);
System.out.println("我获得的信息是:" + "chCx=" + x + chCx + ",dBx=" + dBx + ",iAx=" + iAx);
}
} public class Example3_15 {
public static void main(String[] args) {
C c = new C(2, 3.0, 'C');
c.method();
}
}

例3.16类A继承扩展类B,类A和类B之间有is-a关系。

class B {
int b; B(int x) {
b = x;
} void write() {
System.out.println("这是来自类B的输出!");
}
} class A extends B {
int a; A(int x, int y) {
super(x);
a = y;
write();
System.out.println("我是子类A!" + "b=" + b + ",a=" + a);
}
} public class Example3_16 {
public static void main(String[] args) {
A obj = new A(1, 2);
}
}

例3.17类A的某个成员变量的类型是B,类A和类B之间是has-a关系。

class A {
B b;
int a; A(int x, int y, int z) {
b = new B(x, y);
a = z;
b.write();
}
} class B {
int b1, b2; B(int x, int y) {
b1 = x;
b2 = y;
} void write() {
System.out.println("这是来自类B的输出!");
}
} public class Example3_17 {
public static void main(String[] args) {
A obj = new A(1, 2, 3);
}
}

例3.18一个含内部类的程序。

class A {
B obj; A() {
obj = new B();
obj.print();
} class B// B是内部类
{
public void print() {
System.out.println("内部类B...");
}
}
} public class Example3_18 {
public static void main(String[] args) {
A obj = new A();
}
}

例3.19声明接口和类实现接口的例子。

interface Computable// 声明一个接口
{
final int MAX = 100; void speak(String s); int f(int x); int g(int x, int Y);
} class A implements Computable// 类A实现Computable接口
{
int no; public int f(int x)// 不要忘记public关键字
{
int sum = 0;
for (int i = 1; i <= x; i++) {
sum = sum + i;
}
return sum;
} public int g(int x, int y) {
return x * y;// 至少有return语句
} public void speak(String s) {
// 必须有方法体,但体内可以没有任何语句
}
} class B implements Computable// 类B也实现Computable接口,但实现的方案不同
{
int no; public int f(int x) {
int sum = 0;
for (int i = 1; i <= x; i++) {
sum = sum + i * i;
}
return sum;
} public int g(int x, int y) {
return x + y;
} public void speak(String s) {
// 必须有方法体,但体内可以没有任何语句
}
} public class Example3_19 {
public static void main(String[] args) {
A Li;
B Tom;
Li = new A();
Li.no = 951898;
Tom = new B();
Tom.no = 951899;
System.out.print("Li的编号:" + Li.no + ",最大值:" + Li.MAX);
System.out.print(",从1到100求和=" + Li.f(100));
System.out.println(",3与4的积=" + Li.g(3, 4));
System.out.print("Tom的编号:" + Tom.no + ",最大值:" + Tom.MAX);
System.out.print(",从1平方加到9平方=" + Tom.f(9));
System.out.println(",5与6的和=" + Tom.g(5, 6));
}
}

例3.20说明小应用程序建立和运行步骤的小应用程序。

import java.applet.*;
import java.awt.*; public class Example3_20 extends Applet {
int pos; public void init() {
pos = 5;
} public void start() {
repaint();
} public void stop() { } public void paint(Graphics g) {
g.drawString("我们正在学习Java程序设计", 20, pos + 10);
pos = (pos + 20) % 100 + 5;
}
}
上一篇:[.net 面向对象编程基础] (2) 关于面向对象编程


下一篇:SAP 传感器辅助定位