Java's basic syntax and so on are very similar to C, so there is no more verbose. Start directly from the array.
Array:
1. Disclaimer
int [] A;
In the declaration process, there is no space allocated to the array. We can use new to allocate space at the same time as we declare.
int [] A = new int[100];
This opens up a space of 100-size arrays, and memory allocations are complete.
We can also determine the number of elements while declaring
int [] A = new int[]{1,3,5,7,9};
Object Oriented (focus)
Class Human
{
int height;
void Breath ()
{
System.out.println ("Hoo-hoo.");
}
}
The human class contains two members: one data member height and the other is Method breath ().
▲ The data member is a property, and the method is the behavior.
Invoking a method or data member through an object
Human person = new Human ();
Person.breath ();
Using the object's private variables through methods
Class Human
{
private int height;
int GetHeight ()
{
return height;
}
}
Main function omitted not written, as output result default height is 0
Human person = new Human ();
Person.height ();
Construction method
Features: 1. Same as class name
2. No return value
Class Human
{
int height;
Human (int h)
{
Height=h;
}
}
Using constructors
Human person = new Human (160); Pass the reference
Constructing method overloads
Class Human
{
int height;
Human (int h)
{
Height=h;
}
Human (int h,string name)
{
Height=h;
SYSTEM.OUT.PRINTLN ("I call" +name);
}
}
Java is constructed based on the parameter selection constructor you passed in.
When the constructor is not written, the system defaults to you using an empty constructor.
Encapsulation and interface
Java controls the external visibility of members of an object by three keywords (visibility): public, private, protected.
- Public: The member is visible externally, that is, the member is part of the interface
- Private: The member is not visible externally and can only be used for internal use and cannot be accessed externally.
(protected involves the concept of inheritance, put it in the future.)
method is generally set to public as an interface, while important data members are generally set to private.
Class Human
{
private int height;
public int getheight ()
{
return height;
}
}
In other words, it is wrong to use height directly outside. The value must be obtained by calling the GetHeight () method
Human person = new Human ();
System.out.println (Person.getheight ());
Interface (interface)
Take the cup as an example, define the interface of a cup
Interface cup{
void Addwater (int h);
void Drinkwater (int h);
}
In the interface, we
- You do not need to define the body of a method
- There is no need to describe the visibility of the method (the interface defaults to public)
We can implement interfaces in the definition of a class, such as the following Musiccup (a cup that can play music):
Class Musiccup implements Cup
{
public void Addwater (int h)
{
Water = water + H;
}
public void Drinkwater (int h)
{
Water = water-h;
}
private int water = 0;
}
When we use an interface, we must write out all the methods in the interface, otherwise we will get an error.
Of course, in addition to using interfaces, we can also define other methods in the class. For example:
Class Musiccup implements Cup
{
public void Addwater (int h)
{
Water = water + H;
}
public void Drinkwater (int h)
{
Water = water-h;
}
public int Getwater ()
{
return water;
}
private int water = 0;
}
Implementing multiple interfaces
A class can implement more than one interface. For example, we have the following interface:
Interface musicplayer{
void Play ();
}
Implement the Musiccup two interfaces
Class Musiccup implements Cup,musicplayer
{
public void Addwater (int h)
{
Water = water + H;
}
public void Drinkwater (int h)
{
Water = water-h;
}
public void Play ()
{
System.out.println ("lala....la");
}
private int water = 0;
}
Inheritance of Classes
Human Class (base class)
Class Human
{
public int getheight ()
{
return height;
}
public void growheight (int h)
{
Height = height + h;
}
public void Breath ()
{
System.out.println ("hu....hu.....hu");
}
private int height;
}
Now we define a woman (Woman) class, use inheritance, use basic features of the base class, avoid duplication
Class Woman extends Human//This time the subclass has all the methods of the parent class (breath (). Growheight (). GetHeight, etc.)
{
Public Human Givebirth ()
{
SYSTEM.OUT.PRINTLN ("Raw Baby");
Return (new Human (20));
}
}
Add the Main method test
Class Test
{
public static void Main (String [] args)
{
Woman Awoman = new Woman ();
Awoman.growheight (120); Methods that call the parent class directly
System.out.println (Awoman.getheight ());
}
}
Method overrides
Class Woman extends Human
{
Public Human Givebirth ()
{
SYSTEM.OUT.PRINTLN ("Raw Baby");
Return (new Human (20));
}
public void Breath ()
{
Super.breath (); Call the breath () method of the parent class
System.out.println ("su ....");
}
}
Note that at this point we are in the derived layer and can still invoke the breath () method of the base class object through Super. When we call the woman class externally, it is no longer possible to call the method of the base class object because of the method override.
After the construction method inherits
After understanding the concepts of the base class object and the derived layer, the construction method of the derived class is easier to understand.
We want to define a construction method with the same name as the class in the definition of the derived class. In this construction method:
- Because the base class object is created and initialized first when the derived object is created, the constructor of the base class should be called first. We can use the Super (argument list) statement to invoke the constructor method of the base class.
- After the base class object is created, start building the derived layer (initializing the derived layer member). This is the same as the general construction method, with reference to construction methods and method overloading
Class Human
{
Public Human (int h)
{
Height = h;
}
private int height;
}
Class Woman extends Human
{
Public Woman (int h)
{
Super (H);
System.out.println ("We let the height of the parent class initialize");
}
}
About static
All objects of the class share the population data. Such data is referred to as class data members (classes field).
In the class definition, we use the static keyword to declare class data members, such as:
Class Human
{
private static int population;
private static Boolean is_mammal = true;
}
We have defined two classes of data members: Population and is_mammal. All human objects share one population data, and the is_mammal (which are mammals) of any human object are true.
Class data members should also set access permissions. For class data members that are declared public, you can use Class.field or Object.field (if there is an object of that class) to access directly from outside the way. Both of these access methods are reasonable, because a class data member can be considered a property of a class and can be considered a property shared by all members. If a class data member is defined as private, the class data member can only be accessed from within the class.
(The Is_mammal is set to public, just for demonstration purposes.) It is dangerous to do so, in case someone uses human.is_mammal=false, all humans suffer. Or that basic principle, try to set the data to private. )
static method
We can also have a class method, that is, a method declared as static. A class method represents an action that a class can implement, where the operation does not involve a specific object. If a method is declared static, it can only invoke static data and methods, not non-static data and methods.
Here we add a static method Getpopulation (), which returns the static data population:
Class Human
{
private static int population;
private static Boolean is_mammal = true;
public static int getpopulation ()
{
return population; Class methods can only access class data members
}
}
The two methods call the class method. Class name. Method or Object. method
Class Test
{
public static void Main (String [] args)
{
System.out.println (Human.getpopulation ()); Direct is the class name. method
Human person = new Human ();
System.out.println (Person.getpopulation ()); Object. Method
}
}
Final definition
The basic meaning of the final keyword is that the data/method/class cannot be changed.
- Final basic type of data: fixed value (constant value), can only be assigned once and can no longer be modified.
- Final method: The method cannot be overwritten. The private method defaults to the final method.
- Final class: The class cannot be inherited.
An object of a normal type can also have the final keyword, which means that the object reference (reference) cannot be modified. That is, the reference can only point to an object. However, the contents of the object can be changed (similar to the static pointer in C). We'll cover the object references later.
If a basic type of data is both final and static, it is a fixed value that stores only one copy. This is ideal for storing some constants, such as Pi.
Interface inheritance
In the implementation interface, we use the interface syntax to separate interface from the class definition to form a body. Interface provides the interface specification for the class.
In the inheritance, we introduce the inheritance mechanism in order to improve the reusability of the program. The inheritance at that time was class-based. The interface interface can also be inherited to extend the original interface.
Interface Inheritance (inheritance) is similar to class inheritance, with the addition of new interface method prototypes based on inherited interface. For example, we use Cup as the original interface:
Interface cup{
void Addwater (int w);
void Drinkwater (int w);
}
On the basis of inheriting cups, we define the interface of a new graduated Cup, Metriccup
Interface Metriccup extends Cup
{
void Getwater (); Return water
}
Multiple Inheritance of Interfacede
In the Java class inheritance, a derived class can have only one base class. In other words, a class cannot inherit more than one class at a time. In Java, interface can inherit more than one interface at a time, which is called multiple inheritance (multiple inheritance).
Like we have a player interface underneath.
Interface player{
void Play ();
}
We have added a Musiccup interface. It has both a cup interface and a player interface, and adds a display () method prototype.
Interface Musiccup extends Player,cup
{
void display ();
}
Abstract class
In life, we have some very abstract concepts. These abstract concepts are often a collection of many classes, such as:
- Grain (can be corn, wheat, rice)
- Graphics (can be triangles, circles, squares)
For example, we have previously cited examples:
- Human (can be a man, a woman)
Abstraction to an image must refine the method in the abstract class, or else the class is abstract
Abstract class Food
{
public abstract void Eat ();
public void Happyfood ()
{
System.out.println ("Good Food");
}
}
A method in a class can be declared abstract, such as Eat () above. At this point, we do not need to define the method, only the prototype of the method needs to be provided. This is similar to an interface. When we inherit this class in the example corn class, we need to provide a concrete definition of the Eat () method.
Another method in the class Happyfood () is not
When an abstract method appears in a class, the declaration of the class must be prefixed with the abstract keyword, or Java will error. An abstract class cannot be used to create an object.
All the content comes from
Vamei Source: Http://www.cnblogs.com/vamei
Notes collation: Ten10
About Java object-oriented basics and personal understanding (for beginners to read)