黄华java课件02

上传人:wt****50 文档编号:45555947 上传时间:2018-06-17 格式:PDF 页数:42 大小:841.82KB
返回 下载 相关 举报
黄华java课件02_第1页
第1页 / 共42页
黄华java课件02_第2页
第2页 / 共42页
黄华java课件02_第3页
第3页 / 共42页
黄华java课件02_第4页
第4页 / 共42页
黄华java课件02_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《黄华java课件02》由会员分享,可在线阅读,更多相关《黄华java课件02(42页珍藏版)》请在金锄头文库上搜索。

1、Java Programming LanguageModule 2Object-Oriented ProgrammingHuang HuaSchool of Computer Frameworks can be used as is or be modified to extend the basic behaviorJava Programming LanguageJava Programming LanguageModule 2, slide 7 of 42Classes as Blueprints for Objects In manufacturing, a blueprint des

2、cribes a device from which many physical devices are constructed. In software, a class is a description of an object: A class describes the data that each object includes. A class describes the behaviors that each object exhibits. In Java technology, classes support three key features of object-orie

3、nted programming (OOP): Encapsulation(封装) Inheritance(继承) Polymorphism(多态)Java Programming LanguageJava Programming LanguageModule 2, slide 8 of 42Declaring Java Technology Classes Basic syntax of a Java class:* class * * * Example:1public class Vehicle 2private double maxLoad; 3public void setMaxLo

4、ad(double value) 4maxLoad = value; 5 6Java Programming LanguageJava Programming LanguageModule 2, slide 9 of 42Associating a New Class With a Package1package finance; / package statement 2 3class Stock 4/ Internals of the class declarations not shown for clarity reason 5The package statement provide

5、s a namespace: First non-comment line Can be omitted resulting in default package Package name used in forming fully qualified name of class, for example finance.Stock To be detailed later.Java Programming LanguageJava Programming LanguageModule 2, slide 10 of 42Declaring the Foreign Classes Used by

6、 the New Class1package finance; 2 3import java.util.Date; 4/ Additional import statements if required go here 5 6class Stock 7/ Implementation of the stock class 8 Where declared? When required? import java.util.*; The java.lang package To be detailed later.Java Programming LanguageJava Programming

7、LanguageModule 2, slide 11 of 42Declaring Attributes Basic syntax of an attribute: * = ; Syntax: data_type identifier; double price; Syntax:data_type identifier = initial_value; double price = 25.50; Syntax: data_type identifier1, identifier2, identifier3; Date birthday, anniversary; Fields represen

8、t attributes (or state) Examples: 1 public class Foo 2privateint x; 3privatefloat y = 10000.0F; 4privateString name = “Bates Motel“; 5Java Programming LanguageJava Programming LanguageModule 2, slide 12 of 42Declaring Methods Basic syntax of a method: * ( * ) * Methods provide behavior. Examples: 1p

9、ublic class Dog 2private int weight; 3public int getWeight() 4return weight; 5 6public void setWeight(int newWeight) 7if ( newWeight 0 ) 8weight = newWeight; 9 10 11 Java Programming LanguageJava Programming LanguageModule 2, slide 13 of 42Accessing Object Members The dot notation is: . This is used

10、 to access object members, including attributes and methods. Examples of dot notation are:d.setWeight(42); / only permissible if weight is public d.weight = 42;Java Programming LanguageJava Programming LanguageModule 2, slide 14 of 42Declaring Constructors Basic syntax of a constructor: ( * ) * Cons

11、tructors provide dynamic initialization of fields. Example:1public class Dog 2 3private int weight; 4 5public Dog() 6weight = 42; 7 8Java Programming LanguageJava Programming LanguageModule 2, slide 15 of 42The Default Constructor There is always at least one constructor in every class. If the write

12、r does not supply any constructors, the default constructor is present automatically: The default constructor takes no arguments The default constructor body is empty The default enables you to create object instances with new Xxx()without having to write a constructor.Java Programming LanguageJava

13、Programming LanguageModule 2, slide 16 of 42The Default Constructor1package finance; 2 3import java.util.Date; 4 5class Stock 6/ Field declarations 7String symbol; 8double price; 9Date date; 10 11/ No constructors declared 12 13/ Method declarations 14 15 Java Programming LanguageJava Programming La

14、nguageModule 2, slide 17 of 42Explicit No-arg Constructor1package finance; 2 3import java.util.Date; 4 5class Stock 6/ Field declarations 7String symbol; 8double price; 9Date date; 10 11/ Constructor declarations 12Stock() 13date = new Date(); 14 15 16/ Method declarations not shown for clarity reas

15、ons 17 18Java Programming LanguageJava Programming LanguageModule 2, slide 18 of 42Overloading Constructors As with methods, constructors can be overloaded. An example is:public Employee(Stringname, double salary, Date DoB) public Employee(Stringname, double salary) public Employee(Stringname, Date

16、DoB) Argument lists must differ. You can use the thisreference at the first line of a constructor to call another constructor.Java Programming LanguageJava Programming LanguageModule 2, slide 19 of 42Overloading Constructors1public class Employee 2private static final double BASE_SALARY = 15000.00; 3private String name; 4private doub

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号