Java object-Oriented programming 1---Class

Source: Internet
Author: User

The concept of a class

A class can inherit everything from other classes

Cases:

Class Employee {
String name;
int age;
float salary;
}
Class Manager extends employee{
String Deparentname;
int deparentnumber;
}

public class test2{
public static void Main (String args[])
{
Manager e=new Manager ();
E.name= "Zhang Xu";
e.age=22;
e.salary=30000;
System.out.println (e.name+ "Age is" +e.age+ "monthly salary for" +e.salary ");

}
}

The adornment of the class (if any)

Public

A public class corresponds to a file that can be called by any class reference

Abstract

cannot be instantiated as an object, only inherited

Final

The final class, which cannot be inherited, prevents the class from being modified and secured, such as the String class.

One

Properties of the class

That is, the member variable, declared format:

[modifier] Variable type variable name [= initial value]

There are three types of modifiers

1 Access Control modifiers

2 static modifier statics

A variable that is modified by it cannot be instantiated as an object, it is intrinsic to the class and can be used without instantiation.

3final

Constant

Declares that a class's properties are in the form of an object: (A variable of one class is an object of another class)

[Modifier] Class name Object name [=new class name (argument list)]

Two

Methods of the class:

Also called member function, through the operation of the properties of the class, is to think of the function of the class, but also the kind of communication channels to communicate with the outside world, realize the information interaction.

Declaration format:

[modifier] Return value type method name (formal parameter list) [throws Exception names Table]

{

}

Method modifiers describe:

Override of Method:

Overridden method: A method in which the parent subclass has the same method name and the same parameter

When a subclass and parent class have the same method, the reference subclass of this method behaves as a method of a subclass.

Purpose: When an overlay method is invoked by a parent reference, Java determines how to execute that version based on the type of object being referenced

Cases:

Class superclass{
public void PrintA () {
System.out.println ("Parent class printing function");
}
}
Class Subclass extends Superclass{
public void PrintA () {
System.out.println ("Sub-class printing function");
}
}
public class test2{
public static void Main (String args[])
{
Superclass S1=new subclass ();
S1.printa ();

}
}

The methods in the subclass are executed at this time.

Overloads of the method:

That is, when a class has more than one method with the same name, and the parameters of these methods are not the same, the method is executed when the method is used, and that parameter matches the corresponding method.

Instantiation of the class:

Examples of references to object properties and methods:

Class person{
int age;
void Shot () {
System.out.println ("Age is:" +age);
}
}
public class test2{
public static void Main (String args[])
{
Person P=new person ();
p.age=23;
P.shot ();
}
}

Parameter passing of the method:

Mengmengdongdong

Java object-Oriented programming 1---Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.