To run the compiled Java file through a DOS command:
1> operation, input cmd;
2> Enter the DOS interface, enter CD: filename path, carriage return
3> input disk, enter
4> Enter javac Java file name. java
5> Enter Java Java file name
Such as:
Encapsulation: to encapsulate objective things into abstract classes, and classes can make their own data and methods only trusted class or object operations, the untrusted object to hide the attributes and implementation details.
The role of encapsulation:
1> Safe and convenient
2> to isolate variability
3> improve the reusability of code
The encapsulation of the property. See the following code:
Class student{
String name;
int age;
public void Show ()
{
System.out.println (name+ ", this year" +age+ ");
}
Public class Lesson {public
static void Main (String args[])
{
Student s=new Student ();
S.name= "Xu Enhanced";
S.setage ( -20);
S.show ();
}
If you do not subscript character The age modifier to private, you can change its value outside of the class, such as on the age=-20, and an error occurs because it is not possible to be negative.
So to hide the attribute of age so that its value cannot be arbitrarily modified outside of the class. You need to add a private modifier.
The naming specification for methods that access private member properties:
1>public void Setxxx (Parameters) provides an interface for assigning values to parameters
2>public type getxxx () get parameter values
The code is modified as follows:
Class student{
String name;
private int age;
public void Setage (int age)
{
if (age>=0 && age<=150)
{
this.age=age;
Show ();
}
else System.out.println ("There is such an age ~");
}
public void Show ()
{
System.out.println (name+ ", this year" +age+ ");
}
public int getage ()
{return age
;
}
}
Public class Lesson {public
static void Main (String args[])
{
Student s=new Student ();
S.name= "Xu Enhanced";
s.age=-20; Age is a private member variable and cannot be accessed directly from the class
s.setage ( -20);
}
Second, the package: similar to the operating system folder.
1> is used to manage multiple Java files in categories.
2> Resolve naming Conflicts (same name, different content), the operating system is case-insensitive for folder names, but package names are case-sensitive.
Definition of packages in Java programs:
Package package name;
Note: Be sure to put the first sentence in the Java program.
Compiling Java files with packages: javac-d. Java files
The full class name of the package must be taken at run time: Java package name. Class Name
Naming specification for package names:
1> If you define a package that has multiple levels: A.B.C.D (that is, a package contains the child package B,b package contains the child Package C ...), the package name is case-sensitive.
2> Package name cannot be named with a pure number.
3> Company to do software development, will generally reverse the URL address as the package name.
such as: Blue Fox Training URL address is: www.bluefox.com.cn.
This time the package is named: Cn.com.bluefox. Project name.
Java programs with packages written with Eclipse as an example:
Create a new lesson project, click SRC, click New, select Package, and create a new package com
, and then create a new child package in the COM package bluefox, as above, and enter Com.bluefox in the first name:
Create the student class in Bluefox, click Com.bluefox Select New\class, and enter the class name student in name:
In the Student class, enter:
Package Com.bluefox;
public class Student {
String name;
int age;
public void Show ()
{
System.out.println ("My name is +name+", this year "+age+");
}
Also create a teststudent class in the COM package, click com Select New\class, and enter the Teststudent class in name.
In the Teststudent class, enter:
package com;
public class Teststudent {public
static void Main (String args[])
{
Student s=new student ();
S.name= "Xu Enhanced";
s.age=20;
S.show ();
}
Compiling teststudent, there is a compile problem:
Student is a class that cannot be resolved, that is, the student class has not been imported in.
Since teststudent is in a COM package, and the student class is not in the same order in the child package Bluefox package in the COM package, the student class needs to be imported into the Teststudent class to run.
Import of packages:
1>import package name, child package .... Class name;
2>import package name. *; Import all the classes in the package in
Importing the student class to the Teststudent class allows you to take advantage of the Eclipse Tools Guide Package Shortcuts: alt+/and Ctrl+shift+o.
package com;
Import com.bluefox.student;
public class Teststudent {public
static void Main (String args[])
{
Student s=new student ();
S.name= "Xu Enhanced";
s.age=20;
S.show ();
}
There are still compile problems after compiling.
Teststudent does not have access to name,age.
Because Name,age is not set to access in the student class, name,age can only be accessed in the same package, or the name,age is set to public to be accessible by classes in other packages.
Student modified as follows:
Package Com.bluefox;
Public class Student {public
String name;
public int age;
public void Show ()
{
System.out.println ("My name is +name+", this year "+age+");
}
To run on it.
Because the default package view in Eclipse is tied, to be clear, you can click the window---navigation---show view menu---Package presentation---hierarchical,
As shown in the following illustration:
Can clearly see the inclusion relationship.
icon in Eclipse:: Source folder (the folders where Java source files are stored).
: Package, bag.
For Bulid path, if you click SRC, click Bulid Path and select Remove to Bulid path, you can see SRC, com, and other packages exist as folders, rather than packages.
This time the compilation will appear:
The compiler does not contain the main type because the Java file is compiled as a source file for Java. So when you encounter this error, remember to SRC as a source folder to compile, click src---
Bulid Path---Use as source folder, this time the compilation can be passed.
Commonly used packages in Java:
1>java.lang.*; Contains basic classes of Java programming, automatic import in program programming
2>java.sql.*; Defines a series of Java-enabled database access support
3>java.util.*; Tool class, assemble frame, data structure, etc.
4>java.net.*; Contains classes that implement network programming
5>java.io.*; Provides system input/output classes for data flow, object sequences, and file systems
6>java.awt.*; Contains classes for creating UI and drawing and image processing, user graphical interface GUI
7>java.swing.*; Ditto.