Classes and objects
member variables, member functions
Special member variables and member functions
function overloading
constructor function
static variables
static functions
Object-oriented: encapsulating inherited polymorphism
Package Encapsulation
Why do I need encapsulation?
The outside world cannot directly manipulate the object's specific properties (security)
Core idea: Define security-demanding member variables as private, operate on them through member functions, and define the conditions for their operation in member functions
Ways to---Management classes: Packages (package)
(1) The class can be placed inside the package,
Note the path of the package on the file header of the class.
Package name;
The customer class is placed in the bank package
It can contain a small package, which is used when defining the package. Separate
The ATM class is placed in the SYS package in the bank package
Package Bank.sys;
Programming Specification: Java, package name all letters lowercase
When a class is managed with a package, classes generally have two ways of accessing it: default mode, public mode
Class customer{} default mode
public class customer{} public mode
The latter is called the public class, and the public class is visible to all classes outside the package
The public class class name must be the same as the file name
Default class, visible only to other classes within the same package
General use of the public class
(2) Use the classes in other packages
Importing classes from other packages
The path of the import class;
Import Bank. Customer; Import the customer class in the bank package
Import bank.*; Import all classes in the bank package, not including the classes in the child package
Packaging:
Class-Level Encapsulation (package)
public class; Default class
Member-level encapsulation
Access control for Members
Private
Default
Protected
Public
/* Member's access control private, default, Protected,public
*private members, visible only within the class
* Default permissions, private visibility + visible to other classes within package
*protected Member, default permissions + visible to package my husband class
*public members, visible to all classes outside the package
* Note: In real projects, member variables are generally private and member functions are generally
*public, members of the inherited use general protected
* Member variables are generally accessed through member functions
*/
You can access a private member variable by defining a setter, getter function
classmanager{Manager () {System.out.println ("Manager"); }} Public classatm{StaticManager m; ATM () {m=NewManager (); System.out.println ("ATM"); } Public Static voidMain (string[] args) {//Customer cus = new customer (); NewATM ();NewATM ();NewATM (); }}
package Bank; public class customer{String account; private double balance = 0; Customer () {}}
Package Bank.sys; Import Bank. Customer; // Import Public class atm{ publicstaticvoid main (string[] args) { new Customer (); }}
PackageBank;classcustomer{String Account; //Default access rights protectedString CNAME;//Protected Access Rights Private DoubleBalance//Private access Rights Public voidQukuan () {}//Public access Rights Public voidCunkuan () {}}/*access control for members private, default, Protected,public *private member, only visible within class * default permissions, private visibility + *protected members visible to other classes within package, default permissions + visible to package my husband class * Public member, visible to all classes outside the package * Note: In real projects, member variables are generally private, member functions are generally *public, and members that are inherited use general protected * General access to member variables through member functions*/
PackageSchool; Public classstudent{PrivateString Stuid; PrivateString sname; Private intAge ; //write setter and getter functions? Public voidsetsname (String sname) { This. sname=sname; } PublicString Getsname () {return This. sname; } Public intGetage () {return This. Age; } Public voidSetage (intAge) { This. Age =Age ;} Public voidSetstuid (String stuid) { This. stuid=Stuid;} PublicString Getstuid () {return This. Stuid;} }classmain{ Public Static voidMain (string[] args) {Student s=NewStudent (); S.setsname ("Zhang San"); System.out.println (S.getsname ()); }}
package Bank.sys; import Bank. Customer; // import public class atm{ public static void Main (string[] args) {Customer cus = new Customer (); }}
package Bank; class customer{String account; private double balance = 0; public void Qukuan (double Mon) { if (balance<< Span style= "color: #000000;" >mon) {System.out.println ( "Not enough money to take" ); return ; } balance -= Mon; System.out.println ( "Take money successfully"
classcustomer{String Account; Private DoubleBalance = 0; Public voidQukuan (DoubleMon) { if(balance<Mon) {System.out.println ("Not enough money to take"); return; } Balance-=Mon; System.out.println ("Take money for Success"); }}classatm{ Public Static voidMain (string[] args) {Customer cus=NewCustomer (); Cus.qukuan (100); Cus.balance=-5000; }}
Packages such as Java