Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / lecture2_1

.pdf
Скачиваний:
39
Добавлен:
14.10.2016
Размер:
853.59 Кб
Скачать

Java Programming Language

Lecture 2.

Class. Class Design. Methods. Variables. Data types.

Brodyagina Mariya

Symbols Used in Defining a Java

Source

– Braces

– Parentheses

Semicolons

Commas

Single quotation marks

– Double quotation marks

– Single-line comment

Class Structure

package <package_name>;

import <other_packages>;

public class ClassName { <variables(also known as fields)>;

<constructor method(s)>;

<other methods>;

}

A Simple Class

A simple Java class with a main method:

public class Simple {

public static void main(String args[]){

}

}

Syntax for declaring a class:

[modifiers] class class_identifier

Java Naming Conventions

Class names are nouns in

upper camel case.

1 public class CreditCard {

2public final int VISA = 5001;

3public String accountName;

4public String cardNumber;

5public Date expDate;

6

 

7

public double getCharges(){

8

// ...

9

}

10

 

Constants should be declared in all uppercase. letters

Variable names are short but meaningful in lower camel case.

11

public void disputeCharge(String chargeId, float amount){

12

// ...

13

}

14 }

Methods should be verbs, in

lower camel case.

 

Comments

// single-line comment /* single-line comment */ /*

* block comment */

statement; // trailing comment

Javadoc comments:

/**

* Javadoc comment */

/** Javadoc comment */

Quiz

Select the class declaration that adheres to the class-naming guidelines.

a.class Shirt

b.public Class 501Pants c.public Shirt

d.public Class Pants

A Simple Java Class: Employee

A Java class is often used to represent a concept.

1

package com.example.domain;

 

2

public class Employee { class declaration

3

public int empId;

 

4

public String name;

fields

5

public String ssn;

 

6

public double salary;

 

7

 

 

8

public Employee () { a constructor

9

}

 

10

 

 

11

public int getEmpId () { a method

12

return empId;

 

13

}

 

14 }

Uses of Variables

Holding unique data for an object instance

Assigning the value of one variable to another

Representing values within a mathematical expression

Printing the values to the screen

Holding references to other objects

Naming a Variable

Rules:

Variable identifiers must start with either an uppercase or lowercase letter, an underscore (_), or a dollar sign ($).

Variable identifiers cannot contain punctuation, spaces, or dashes.

Java technology keywords cannot be used.

Соседние файлы в папке Lectures