210 likes | 370 Views
Программирование на Java: Концепция ООП. Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ. Content. intro. what an object is what a class is how objects and classes are related how objects communicate by using messages. What Is an Object?. Everything around you are objects
E N D
Программирование на Java: Концепция ООП Вячеслав Гребенюк ЦТДО, каф. ИИ, ХНУРЭ
Content (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
intro • what an object is • what a class is • how objects and classes are related • how objects communicate by using messages (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is an Object? • Everything around you are objects • Real-world objects share two characteristics: state and behavior • Software objects: variable and method Definition: An object is a software bundle of variables and related methods. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is an Object? (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
First principle of OOP • Encapsulation – packaging an object's variables within the protective custody of its methods • Two major benefits to software developers: • Modularity • Information-hiding (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is a Message? (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Two important benefits of messages • An object's behavior is expressed through its methods, so (aside from direct variable access) message passing supports all possible interactions between objects. • Objects don't need to be in the same process or even on the same machine to send messages back and forth and receive messages from each other. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is a Class? • Object is an instance of the class of objects • A software blueprint for objects is called a class Definition: A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
The bicycle class (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Class variable and methods (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is Inheritance? (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is Inheritance? • Superclass • Subclasses • Inherits • Override • Class hierarchy • The Object class is at the top of class hierarchy (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Benefits of inheritance • Reuse the code in the superclass many times • Abstract classes that define common behaviors (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
What Is an Interface? • Interface is a type • Like a class, an interface defines methods • Unlike a class, an interface never implements methods • Classes that implement the interface implement the methods defined by its • A class can implement multiple interfaces (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Language Basics • public class BasicsDemo { public static void main(String[] args) { int sum = 0; for (int current = 1; current <= 10; current++) { sum += current; } System.out.println("Sum = " + sum); }} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables • Definition: A variable is an item of data named by an identifier • Variable declaration:type name; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example • public class MaxVariablesDemo { public static void main(String args[]) { //integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; //real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; //other primitive types char aChar = 'S'; boolean aBoolean = true; • //Display them all. System.out.println("The largest byte value is “+ largestByte + "."); System.out.println("The largest short value is “+ largestShort + "."); (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example cont. • System.out.println("The largest integer value is “ + largestInteger + "."); System.out.println("The largest long value is “ + largestLong + "."); System.out.println("The largest float value is “ + largestFloat + "."); System.out.println("The largest double value is “ + largestDouble + "."); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is uppercase."); } else { System.out.println("The character " + aChar + " is lowercase."); } System.out.println("The value of aBoolean is “ + aBoolean + "."); }} (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Variables example results • The largest byte value is 127. • The largest short value is 32767. • The largest integer value is 2147483647. • The largest long value is 9223372036854775807. • The largest float value is 3.4028235E38. • The largest double value is 1.7976931348623157E308. • The character S is uppercase. • The value of aBoolean is true. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
More about variables • Data Types • Variable Names • Scope • Variable Initialization • Final Variables (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006