面向对象程序设计概述面向过程:程序=算法+数据结构
面向对象:程序=对象+对象+…
面向对象将问题域划分成一个个对象,并将与特定问题相关的算法与数据结构封装在特定的对象中。
类类是构造对象的模板。
类的三个主要特性:
封装,继承,多态。
对象对象是类的实例。
对象的三个主要特性:
行为,状态,标识。
使用预定义类Java类库中有上千种类,要想使用它们,首先构造对象,指定其初始状态,然后,对对象应用方法。例:
Date birthday = new Date();
String s = new Date().toString();
对象与对象变量对象是堆内存中的实体。
对象变量存在于栈内存,是对对象实体的引用,或者说对象变量的值就是对象实体的内存地址。
更改器方法与访问器方法通常给访问器方法名加前缀get,get方法仅查看并返回对象的状态。
通常给更改器方法名加前缀set,set方法对对象的状态进行修改。
- import java.text.DateFormatSymbols;
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- GregorianCalendar d = new GregorianCalendar();
-
- int today = d.get(Calendar.DAY_OF_MONTH);
- int month = d.get(Calendar.MONTH);
-
- d.set(Calendar.DAY_OF_MONTH, 1);
-
- int weekday = d.get(Calendar.DAY_OF_WEEK);
-
- int firstDayOfWeek = d.getFirstDayOfWeek();
-
- int indent = 0;
- while (weekday != firstDayOfWeek)
- {
- indent++;
- d.add(Calendar.DAY_OF_MONTH, -1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
- }
-
- String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
- do
- {
- System.out.printf("%4s", weekdayNames[weekday]);
- d.add(Calendar.DAY_OF_MONTH, 1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
- }
- while (weekday != firstDayOfWeek);
- System.out.println();
- for (int i = 1; i <= indent; i++)
- System.out.print(" ");
-
- d.set(Calendar.DAY_OF_MONTH, 1);
- do
- {
- int day = d.get(Calendar.DAY_OF_MONTH);
- System.out.printf("%3d", day);
-
- if (day == today)
- System.out.print("*");
- else
- System.out.print(" ");
-
- d.add(Calendar.DAY_OF_MONTH, 1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
-
- if (weekday == firstDayOfWeek)
- System.out.println();
- }
- while (d.get(Calendar.MONTH) == month);
-
- if (weekday != firstDayOfWeek)
- System.out.println();
- }
- }
复制代码 用户自定义类
class ClassName
{
field1
field2
…
constructor1
constructor2
…
method1
method2
…
}
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee[] staff = new Employee[3];
- staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
- staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
- staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
-
- for (Employee e : staff)
- e.raiseSalary(5);
-
- for (Employee e : staff)
- System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay());
- }
- }
- class Employee
- {
- private String name;
- private double salary;
- private Date hireDay;
-
- public Employee(String n, double s, int year, int month, int day)
- {
- name = n;
- salary = s;
- GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
- hireDay = calendar.getTime();
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public Date getHireDay()
- {
- return hireDay;
- }
-
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
- }
复制代码 静态域与静态方法
静态域可以将类中的域分为静态域和实例域。
实例域是指本类所有对象都各自拥有的域。
静态域是指本类所有对象都共享的域。
实例域属于对象,存储在堆中。
静态域属于类,存储在静态存储区中。
只要类已经载入内存,即使还没有创建对象,静态域也已经存在了。
静态方法可以将类中的方法分为实例方法和静态方法。
调用实例方法,须用对象.实例方法的形式,调用时,将在栈内存中开辟空间。
调用静态方法,可用类名.静态方法的形式,静态方法随着类的加载已经存在于静态存储区,不会在调用时开辟内存。
静态方法可以访问本类的静态域,但不能访问本类对象的实力域。
main方法每一个类可以有一个main方法,这个特性常用于对类进行单元测试。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee[] staff = new Employee[3];
- staff[0] = new Employee("Tom", 40000);
- staff[1] = new Employee("Dick", 60000);
- staff[2] = new Employee("Harry", 65000);
-
- for (Employee e : staff)
- {
- e.setId();
- System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
- }
-
- int n = Employee.getNextId();
- System.out.println("Next available id=" + n);
- }
- }
- class Employee
- {
- private static int nextId = 1;
-
- private String name;
- private double salary;
- private int id;
-
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- id = 0;
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public int getId()
- {
- return id;
- }
-
- public void setId()
- {
- id = nextId;
- nextId++;
- }
-
- public static int getNextId()
- {
- return nextId;
- }
-
- public static void main(String[] args)
- {
- Employee e = new Employee("Harry", 50000);
- System.out.println(e.getName() + " " + e.getSalary());
- }
- }
复制代码
|
|