静态和非静态方法的调用+三元运算
方法
- 
静态方法 未加了static 
- 
非静态方法 加了static 非静态方法的调用 //文件夹1
 package com.opp.Demo01;
 
 public class demo02 {
 public static void main(String[] args) {
 // 类名.方法
 student.say();
 }
 }
//文件夹2
package com.opp.Demo01;
public class student {
    //非静态方法
    public static void say(){
        System.out.println("学生说话了");
    }
}
静态方法的调用
没有static的话,文件夹一无法用 “类名.方法”调用2中的方法;需要我们实例化2中的类
具体操作如下
文件夹1
package com.opp.Demo01;
public class demo02 {
    public static void main(String[] args) {
        //实例化类
        //对象类型   对象名 = 对象值
        student t = new student();
        t.say();
        //或者直接  new student().say();
    }
}
文件夹2
package com.opp.Demo01;
public class student {
    //静态方法
    public  void say(){
        System.out.println("学生说话了");
    }
}
加法 静态和非静态的比较
package com.opp.Demo01;
//非静态
public class demo02 {
    public static void main(String[] args) {
        int add =  demo02.add(1, 2);
        System.out.println(add);
}
public  static int add (int a,int b){
        return a+b;
}
}
package com.opp.Demo01;
//静态;要实例化,new
public class demo02 {
    public static void main(String[] args) {
        int add =  new demo02().add(1, 2);
        System.out.println(add);
}
public   int add (int a,int b){
        return a+b;
}
}
//三元运算符   如果a>b输出a否则输出b
int n= 1;
int m = 2;
int p = 7;
 System.out.println(n>m ? n : m);  //2
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号