Objective
Java Basics-Classes and objects, overloads of methods, overloading of constructor methods, static keyword, main () method, this keyword, package, access permission, class inheritance, inheritance, method override, Super variable.
Overloading of methods: overloading of member methods and overloading of constructor methods
Overloading of methods
In a class, you can have multiple methods with the same name, but these methods have different arguments, or different types of parameters, which are overloads of the method.
class Demo{ // one void dashu(String name){ System.out.println(name); } // two void dashu(String name,String score){ System.out.println(name+score); } // three void dashu(String name,String score,Int age){ System.out.println(name+score+age); } public static void main(String[] args){ Demo demo = new Demo(); demo.dashu("dashucoding"); demo.dashu("dashucoding","一百"); demo.dashu("dashucoding","一百",10); }}
Overloading of construction methods
class Person{ String name; int age; float height; float weight; // 构造方法 public Person(){ age = 13; name="dashu" } public Person(String name){ this.name = name; } // 构造方法的重载 public Person(String name, int age){ this.name = name; this.age = age; } void print(){ System.out.println(name+age); }}class Test{ public static void main(String[] args){ Demo demo1,demo2,demo3; demo1 = new Demo(); demo2 = new Demo("dashu"); demo3 = new Demo("dashu",12); demo1.print(); demo2.print(); demo3.print(); }}
Static keyword
staticthe modified variable is a static variable
staticthe modified method is a static method
A static variable belongs to a class and does not belong to an instance of the class, and can be called by a direct class name, so called a class variable
A static method belongs to a class and not to an instance of a class, and can be called by a direct class name, so called class method
Non-static member variables and methods that must be called by the object name after instantiation
So called instance variables and instance methods
Instance variables and instance methods are called through an instance, and class variables and class methods can also be called through an instance
This should be noted
Main () method
main()method is static and java can be called when executing a method in a virtual machine main() without creating an main() instance of the class where the method resides, because there is a static modification.
Example
class Demo{ // 定义静态的成员变量 static int x; int y; // 定义静态的成员方法,用来获取值 public static int getX(){ return x; } // 定义静态的成员方法,用来设置值 public static void setX(int newX){ x = newX; } public int getY(){ return y; } public void setY(int newY){ y = newY; }}
Static methods can call static members of the same kind, cannot invoke non-static members, and vice versa; static members can be called through the class name or through instance objects, and cannot be used in static methods this super .
this keyword
thisRepresents the current object that uses the method, and is used only in instance methods or construction methods.
void setAge(int age){ this.age = age;} Person(String name, int age){ this.name = name; this.age = age; }
Package
javadifferent classes and interfaces are provided in the different packages.
Common packages
java.appletjava.awtjava.netjava.utiljava.langjava.io
Format of package:
package 包名[.子包名[…]];
Introducing Java Packages
import 包层次结构的类名;
Access rights
Modifier:
public、private、protected、友好
privateVariables and methods
Access is minimal, accessible only in the same class, private members can be called by object instances, methods are called by object instances, and static members can be called directly by the class name.
class Demo{ private static String name; private int age; private void print(){ System.out.println(name+age); } public static void main(String[] args){ Demo.name = "dashu"; Demo d = new Demo(); d.age = 12; d.print(); }}
private, private variables in a class cannot be referenced by another class, and private variables and methods can only be used in this class.
class Demo{ private static String name; private int age; private void print(){ System.out.println(name+age); } public static void main(String[] args){ Demo.name = "dashu"; Demo d = new Demo(); d.age = 12; d.print(); }}// 另一类class Da{ public static void main(String[] args){ // 非法使用 Demo.name = "dashu"; // 非法调用 Demo d = new Demo(); d.print(); }}
publicAccess modifiers, which access permissions in different classes, in different package classes
默认,友好Permissions can only be referenced by the same class or by different classes in the same package
protectedYou cannot reference a class in a different package
| access Rights |
| Public
protected |
Friendly |
Private |
| Similar |
OK |
OK |
OK |
OK |
| Different classes of the same package |
OK |
OK |
OK |
|
| different bun types |
OK |
OK |
|
|
| General |
OK |
|
|
|
Inheritance of Classes
In java can be single-inheritance, multi-inheritance is not supported, a subclass can have only one parent class, cannot have more than one, but a parent class can have more than one derived subclass.
public class Person{ String name; int age; public void setAge(int age) { this.age=age; } public void out() { System.out.println("姓名:"+name+" 年龄:"+age+"岁"); }}// 子类继承父类public class Student extends Person{ ...}
Inheritance of
Subclasses inherit the parent class and can inherit non-private members of the parent class (the child and parent classes are in the same package), and if not in the same package, the subclass can inherit only the protected and public members of the parent class.
Override of Method
Method overrides, subclasses override methods of the parent class, define methods with the same name, same type, and same parameters as the parent class.
Super variables
super, provides access to the parent class, and can access hidden variables and overridden methods of the parent class quilt class.
Conclusion
- This article mainly explains? Java Basics-Classes and Objects
- Below I will continue to
Java ,? Android In-depth explanation of other knowledge, and interested to continue to focus on
- A little gift to walk or like.
Section 31st: Java Basics-Classes and objects