1. Define a class named, HelloWorld, case sensitive
Class HelloWorld
{
// The beginning of the program is fixed, remember
Public static void main (string args [])
{
/ / Output statement, output HelloWorld
System.out.println ("Hello World");
}
}
2.
Class Person
{
Int agec;
String name;
/ / Define a () method, return value is an integer, there are two parameters
Int A (INT I, INT J)
{
// i × j value assigns to age agge
AGE = i * j;
/ / Output Name and age
The age of System.out.Println (Name "is" AGE);
Return 100;
}
// There is a main method, so this class can be implemented.
Public static void main (string [] args)
{
Person P = New Person ();
P.AGE = 20;
p.Name = "Li Si";
// assign a variable using the return value
// Call a method, pay attention to the parameter to be consistent with the defined format, two parameters
INT ABC = P.A (3, 7);
System.out.println (ABC);
// Create another person, P2
Person P2 = New Person ();
P2.AGE = 30;
P2.NAME = "Zhang San";
The age of System.out.Println (p2.name "is" p2.age);
// p and P2 two person data are separated.
The age of System.out.Println (p.Name "is" P.AGE);
}
}
3.
/ / Define a student class, including properties and methods
Class Student
{
/ / Define an integer attribute, indicating age
Int nianling;
/ / Define the properties of a string, indicating that the name
String Xingming = "abcd";
/ / Define a method, the method must have a return value, and () and method content
Public void chifan ()
{
// Connect the name and a "string" together, output
System.out.println (xingming "in eating");
}
// Write a constructor, if not, it will automatically add a constructor
Public student ()
{
System.out.println ("A Student is Creating");
}
}
4.
Class test
{
Public static void main (string [] args)
{
STUDENT ZHANGSAN;
/ / Construct a student ZhangSan call constructor, create a student
();
Huhangsan.chifan ();
/ / Change the name of ZHANGSAN. Attribute name
Zhangsan.nianling = 30;
/ / Change the name of Zhangsan
ZHANGSAN.XINGMING = "Zhang San";
// Call ZhangSan's Chifan method object name. Method name
Huhangsan.chifan ();
The age of system.out.println (zhangsan.xingming "is" zhangsan.nianling);
}
}