Java主类的main方法调用其他方法

方法1: funA()方法设为静态方法。 当主类加载到内存,funA()分配了入口地址,主要代码如下:
public class test{
static void funA(){
System.out.println("we are students");
}
public static void main(String args[]){
System.out.println("Hello, 欢迎学习JAVA");
funA(); //使用静态方法
}
}
方法2: class A与 主类并列,如下
public class testDemo {
/*
* 成绩统计
* */
public static void main(String[] args) {
Integer[] chinaScore = {,,,,};
int count=;
//合计值
for(int i = ;i<chinaScore.length;i++) {
count+=chinaScore[i];
}
System.out.println("总成绩:"+count); //平均值
int scoreAvg = count/chinaScore.length;
System.out.println(scoreAvg); //保留2位小数
DecimalFormat df = new DecimalFormat(".00");
System.out.println(df.format(scoreAvg)); //最大值 int min = (int) Collections.min(Arrays.asList(chinaScore));
int max = (int) Collections.max(Arrays.asList(chinaScore));
System.out.println("历史最高分:" + max);
funA();//调用
testClass otherFun = new testClass();//使用外部类
otherFun.vo();
} /*
* 自定义funA()函数静态方法
* **/
static void funA() {
System.out.println("we are students");
}
} //class A与 主类并列,main方法中调用其他类的函数
class testClass{
void vo() {
System.out.println("你很帅");
}
}
方法3:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生
public class test{
class A{
void fA(){
System.out.println("we are students");
}
}
public static void main(String args[]){
System.out.println("Hello, 欢迎学习JAVA");
A a=new test().new A(); //使用内部类
a.fA();
}
}

Java 怎样实现调用其他方法的更多相关文章

  1. 关于Java中子类调用父类方法

    当一个类继承于另一个类,子类中没有父类的方法时.用子类的对象调用方法时,会首先在子类中查找,如果子类中没有改方法,再到父类中查找. 当一个方法只在父类中定义时,调用该方法时会使用父类中的属性. 如果该 ...

  2. java 多态 ---父类调用子类方法

    package test1;//多态的体现import javax.print.attribute.standard.RequestingUserName;import java.util.Scann ...

  3. Java学习----到底调用哪一个方法(多态)

    public class Father { public void print() { System.out.println("Father:print()"); } } publ ...

  4. java泛型类型变量能调用的方法

    public class Person { } public class Student extends Person{ private String name; public Student(Str ...

  5. Java反射机制调用私有方法

    1.获取目标类: 每个类都有一个class属性,通过实体类的class属性获取: Class clazz = Person.class 通过对象获取.  Person p1 = new Person( ...

  6. java高级用法之:调用本地方法的利器JNA

    目录 简介 JNA初探 JNA加载native lib的流程 本地方法中的结构体参数 总结 简介 JAVA是可以调用本地方法的,官方提供的调用方式叫做JNI,全称叫做java native inter ...

  7. 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题

    1.有关调用的一些问题: (1).在C#中直接调用java中的代码,无返回值: 在java中: public static void setAge(Context context , int leve ...

  8. paip。java 高级特性 类默认方法,匿名方法+多方法连续调用, 常量类型

    paip.java 高级特性 类默认方法,匿名方法+多方法连续调用, 常量类型 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http ...

  9. java调用本地方法的时候报错 could not find the main class:xx.program will exit

    如图所示,当在java调用本地方法的时候报错 我的解决办法是把dll文件放到System.out.println(System.getProperty("java.library.path& ...

随机推荐

  1. 牛客多校第九场 B Quadratic equation 模平方根

    题意: 已知 $x+y$ $mod$ $q = b$ $x*y$ $mod$ $q = c$ 已知b和c,求x和y 题解: 容易想到$b^2-4c=x^2-2xy+y^2=(x-y)^2$ 那么开个根 ...

  2. P1566 加等式

    P1566 加等式 题目描述 对于一个整数集合,我们定义“加等式”如下:集合中的某一个元素可以表示成集合内其他元素之和.如集合{1,2,3}中就有一个加等式:3=1+2,而且3=1+2 和3=2+1是 ...

  3. window location跳转

    "top.location.href"是最外层的页面跳转"window.location.href"."location.href"是本页面 ...

  4. 剑指offer——29顺时针打印矩阵

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...

  5. LeetCode 67. Add Binary【个位补0,不必对齐】【easy】

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. 【洛谷】P1009阶乘之和

    题目链接:https://www.luogu.org/problemnew/show/P1009 题意:给一个整数n(n<50),求$ \sum^{n}_{i=1} i! $ 题解:我..拿py ...

  7. shell对比两个目录中有哪些文件不同

    参考:https://www.cnblogs.com/liyuchuan/p/10756066.html 目录结构: /data/dir1 subdir/ subfile /data/dir2 sub ...

  8. Java超简明入门学习笔记(一)

    Java编程思想第4版学习笔记(一) 第二章 一切都是对象(Hello World)          这个笔记本主要记录了我在学习Java编程思想(第4版,中文版)的过程中遇到的重难点及其分析.主要 ...

  9. Read-Write lock 看可以,不过看的时候不能写

    当线程“读取”实例的状态时,实例的状态不会改变,只有线程对实例“写入”操作时才会改变.read-write lock 模式将读取和写入分开来处理,在读取数据前获取读锁定,而写入之前,必须获取写锁定. ...

  10. kcptun搭建

    wget --no-check-certificate https://github.com/kuoruan/shell-scripts/raw/master/kcptun/kcptun.sh sha ...