方法名:注意规范就好(见名知意)
静态方法和非静态方法:
static在方法中的作用:
package com.Demo;
public class Demo01 {
//静态方法
public static void SayHello(){
System.out.println("HelloWorld!");
}
}
package com.Demo;
public class Demo02 {
public static void main(String[] args) {
//直接调用方法
Demo01.SayHello();
}
}
package com.Demo;
public class Demo01 {
//非静态方法
public void SayHello(){
System.out.println("HelloWorld!");
}
}
package com.Demo;
public class Demo02 {
public static void main(String[] args) {
//实例化这个类
//对象类型 对象名 = 对象值;
new Demo01().SayHello();
//或者下面也行
Dome01 demo = new Dome01();
demo.SayHello();
}
}
package com.Demo4;
public class Action {
public static void main(String[] args) {
}
//静态方法
public static void a(){
b();//报错
}
//非静态方法
public void b(){
a();//正确
}
}
总结:
形参和实参
package com.Demo;
public class Demo {
public static void main(String[] args) {
//实参
int sum = Demo03.Multiplication(1, 2);
System.out.println(sum);
}
//形参
public static int Multiplication(int a,int b){
return a*b;
}
}
值传递和引用传递
public class Demo {
public static void main(String[] args) {
int a = 1;
System.out.println(a);//a=1
Demo04.change(a);
System.out.println(a);//a=1
}
public static void change(int a) {
a = 2;
}
}
package com.Demo;
//引用传递:对象,本质还是值传递
public class Demo {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.name);//null
Demo05.change(p);
System.out.println(p.name);//Tom
}
public static void change(Person p){
//p是一个对象:指向的---> Person p = new Person();这是一个具体的人,可以改变属性
p.name= "Tom";
}
}
class Person{
String name;
int age;
}
//person类
package com.Demo;
public class Person {
protected String name = "Tom111";
}
//Students类
package com.Demo;
public class Students extends Person {
private String name = "Tom222";
public void say(String name){
System.out.println(name);//Tom333
System.out.println(super.name);//Tom111
System.out.println(this.name);//Tom222
}
}
//测试类
package com.Demo;
public class Test {
public static void main(String[] args) {
Students s = new Students();
s.say("Tom333");
}
}
//父类
package com.Demo;
public class Person {
public Person() {
System.out.println("父类方法调用了");
}
}
//子类
package com.Demo;
public class Students extends Person {
public Students() {
//默认有个super()
super();
System.out.println("子类方法调用了");
}
}
//主函数
package com.Demo;
public class Test {
public static void main(String[] args) {
Students s = new Students();
}
}
//输出:
父类方法调用了
子类方法调用了
权限:
public
protected
default
private
public class Test {
public static void main(String[] args) {
//Object-->People-->Teacher
// Object-->People-->Studnets
Object o = new Students();
System.out.println(o instanceof Students);//ture
System.out.println(o instanceof People);//ture
System.out.println(o instanceof Teacher);//false
System.out.println(o instanceof Object);//ture
System.out.println("===============================");
People p = new Students();
System.out.println(p instanceof Students);//ture
System.out.println(p instanceof People);//ture
System.out.println(p instanceof Teacher);//false
System.out.println(p instanceof Object);//ture
System.out.println("===============================");
Students s = new Students();
System.out.println(s instanceof Students);//ture
System.out.println(s instanceof People);//ture
System.out.println(s instanceof Object);//ture
}
}
//变量
pravatic static int age;
pravatic int money;
Student s = new Student();
//调用:
s.money;
Student.age;
//方法
public static void Hello(){
}
//匿名
{
//代码块(匿名代码块)
}
//静态(只执行一次)
static{
//静态代码块
}
//构造
public person(){
}
//加载的顺序:静态>匿名>构造
定义:在一个类中,再定义一个类
内部类可以获取外部类属性。
原文:https://www.cnblogs.com/starsupreme/p/14724192.html