a2水 Lecture4 4-1 Java语言基础 Programming in Java JAVA
4-1 Programming in Java Java 语言基础 Lecture4
a2水 提纲 4-2 ·Java程序剖析 ·Java语言基础 >字符集 >类型 》语句 Programming in Java JAVA
4-2 Programming in Java 提纲 • Java 程序剖析 • Java 语言基础 ➢ 字符集 ➢ 类型 ➢ 语句
简单的Java程序 4-3 Java源程序由类定义组成,每个程序中可以 定义若干个类,但只有一个是主类 ·程序类:该类的一个对象定义程序应该做的 工作 ·主类:告诉Java解释器如何运行程序 Programming in Java JAVA
4-3 Programming in Java 简单的Java 程序 • Java 源程序由类定义组成,每个程序中可以 定义若干个类,但只有一个是主类 • 程序类: 该类的一个对象定义程序应该做的 工作 • 主类: 告诉 Java 解释器 如何运行程序
a2k 一个简单的美的定义 4-4 Choose a name public class ClassName Data field declarations (constants and variables) Constructor declarations Method declarations Stored in a file called ClassName.java Programming in Java JAVA
4-4 Programming in Java public class { } 一个简单的类的定义 Choose a name ClassName Data field declarations (constants and variables) Constructor declarations Method declarations Stored in a file called ClassName.java
孟昊的定义 4-5 public class ClassNameRunner public static void main(String[]args) Statements to construct a program object and ask it to perform computations ] Stored in a file called ClassNameRunner.java Programming in Java JAVA
4-5 Programming in Java public class { public static void main(String[] args) { } } 主类的定义 ClassNameRunner Statements to construct a program object and ask it to perform computations Stored in a file called ClassNameRunner.java
main方法 4-6 When you give a class to the Java interpreter it looks for a main method in the class that has the prototype public static void main(String[]args) and begins executing the statements in it. Programming in Java JAVA
4-6 Programming in Java main 方法 • When you give a class to the Java interpreter it looks for a main method in the class that has the prototype public static void main(String[] args) and begins executing the statements in it
示俐:Circle calculator 4-7 程序构造一个称为circle的CircleCalculator 对象 。程序要求circle读入radius 程序要求circle计算area和circumference 程序要求circle显示radius,,area和 circumference Programming in Java JAVA
4-7 Programming in Java 示例:Circle calculator • 程序构造一个称为circle的 CircleCalculator 对象 • 程序要求circle 读入 radius • 程序要求circle 计算area 和 circumference • 程序要求circle 显示radius, area 和 circumference
CircleCalculatorRunner class 4-8 public class CircleCalculatorRunner f public static void main(String[]args) CircleCalculator circle new circleCalculator () construct send messages objec to circle.readInput () the circle object circle.doCalculations() circle.displayResults () Programming in Java JAVA
4-8 Programming in Java CircleCalculatorRunner class public class CircleCalculatorRunner { public static void main(String[] args) { CircleCalculator circle = new CircleCalculator(); circle.readInput(); circle.doCalculations(); circle.displayResults(); } } construct object send messages to the circle object
a2水 构造一个对象 4-9 new circleCalculator ( 调用这些语句 public CircleCalculator() { Statements } Programming in Java JAVA
4-9 Programming in Java 构造一个对象 new CircleCalculator() 调用这些语句 public CircleCalculator() { } Statements
CircleCalculator class 4-10 public class CircleCalculator data field { declarations private double radius; private double area; private double circumference; fill in constructor public CircleCalculator(){...} details public void readInput(){...} Pu void((. fill in method details Programming in Java JAVA
4-10 Programming in Java CircleCalculator class public class CircleCalculator { private double radius; private double area; private double circumference; public CircleCalculator() {...} public void readInput() {...} public void doCalculations() {...} public void displayResults() {...} } data field declarations fill in constructor details fill in method details