Thursday, July 10, 2014

In the example above, we define a class of a speaker where you can control the volume (instance fie

Basic object-oriented programming in Java: Introduction and syntax | ToggleOn - where all nodes leads.
Basic object-oriented programming in Java: Introduction and syntax 12 May 2010 at. 12:29 AM | Posted in Java, Programming Concepts | 6 comments Labels: Instance Fields, Instance Methods, Java, Class Field, Class Methods, Object Oriented Programming, Object Orientation, OOP, Programming
This is a series of posts that will deal with basic object-oriented programming in Java. The planned interventions are: Introduction and Syntax Constructors and methods Encapsulation Inheritance and subclasses interfaces and abstract classes
Note that here we will only address the basics of object-oriented Java. These posts are then targeted to beginners of object orientation, which have some insight into programming with Java.
It is important to know the terminology, kmno4 in both Swedish and English. I will therefore in many places to print a concept kmno4 in Swedish followed by the term in English, inside parentheses, eg:
Object-oriented programming (OOP) is a paradigm in which objects interact with each other. These objects are so-called state (U.S. state), which consists of its data and methods. Data and methods can be encapsulated (U.S. encapsulation), which means that they can not be accessed outside the object. Methods invoked by the object so as to access the data contained therein. To do this, the methods to be public, which means that they are visible to the user of the object. Usually, for data to be private and the methods public. Methods in itself kmno4 may also be encapsulated, and thus can then only be used by the object. A method is a function that can be used to return data from an object or change in an object.
An object kmno4 is an instance of a class. A class is a kind of description kmno4 of how an object should look like. Items can then be created from this description. Classes can inherit properties from higher classes, in a so-called inheritance hierarchy. kmno4 This is a 'is-relationship. One can e.g. say that a class Car is a subclass of the higher class vehicles. Vehicles could be implemented as an abstract class. You can not create any objects of abstract classes - we want to eg not able to create any objects of vehicles, then Vehicle is an abstract description of the properties and methods of a vehicle has. Instead, we create subclasses Car, Truck, etc. and creates objects of these.
We will throughout this series to dive deeper into what object kmno4 orientation is. It's wide field and is different in different kmno4 languages, depending on how it is implemented. In Java, it is a bit special, in the sense that object orientation is built into the language. Class and Instance Syntax
Class declarations can include zero or more modifiers. These written words before the class keyword: public A class that is visible to the classes defined outside of its package. An abstract class that can not be instantiated, so that objects can be created kmno4 of the class. However, it is possible to inherit from an abstract class and create object of the inheriting class. A final class as opposed to the abstract may not be inherited. This enables the Java VM to optimize its methods. strictfp All methods behave as if they were declared kmno4 with strictfp. It is rare that this is used in practice.
An object's data is usually in Java called its fields (eng. field). There are two types of data and methods in Java: data and methods that are associated with each separate object, and then there are data and methods associated with the class itself. The latter is therefore called kmno4 class fields kmno4 and class methods. This gives us four types of members: Class Field Class Methods Instance Fields Instance methods
Example: A class 'Speaker' containing instance fields and instance methods public class Speaker {/ / Instance Fields public int volume; / / Instance Methods public void increaseVolume (int increaseWith) {this.volume + = increaseWith; } Public void decreaseVolume (int decreaseWith) {this.volume - = decreaseWith; } Public void setVolume (int volume) {this.volume = volume; GetVolume} public int () {return this.volume; kmno4 } Public static void main (String [] args) {Speaker speaker = new Speaker (); speaker.setVolume (5); speaker.increaseVolume (10); System.out.println (speaker.getVolume ()); }}
In the example above, we define a class of a speaker where you can control the volume (instance field volume) with any of the defined instance methods. In main (), we create an instance of the Speaker and set its volume to 5. Subsequently, we increase the volume by 10, which gives the volume 15.
Example: A class containing only class fields and class methods public class MathUtils {/ / Class Fields public static final double PI = 3.1415; / / Class Methods public static double mean (int [] p) {int sum = 0; for (int next_element: p) {sum + = next_element; } Return ((double) sum) / P.le

No comments:

Post a Comment