回顾方法
- 下面一段代码有两个方法,第一个方法sayhello()用来输出一段字符串"hello,world",第二段代码则使用三目运算来比较两个数大小。我们应该记住,书写方法时我们必须要定义返回值的类型。
package oop.demo01;
public class Demo01 {
public static void main(String[] args) {
}
public String sayHello(){
return "hello,world";
}
public int max(int a,int b){
return a>b?a:b;
}
}
- 下面两段代码,在第一段代码中去调用第二段代码,调用第二段代码中的方法是非静态(没有static关键字),所以首先要做的就是实例化:
Student student = new Student();
然后再调用方法:student.say();
package oop.demo01;
public class Demo02 {
public static void main(String[] args) {//如果调用的是静态方法则可以直接:类名.方法(实参)
//调用Student类,当方法不加static,即非静态方法时,要实例化这个类,如下
Student student = new Student();
student.say();
}
public static void a(){//同为静态或非静态的方法可以直接调用
b();
}
public static void b(){
}
}
package oop.demo01;
public class Student {
public void say(){
System.out.println("学生说话了");
}
}
- 简单的一个调用add方法:
其中实例化步骤一定不能省略。
package oop.demo01;
public class Demo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
int add= demo03.add(1,2);
System.out.println(add);
}
public int add(int a,int b){
return a+b;
}
}
- 深入了解java中方法返回值
package oop.demo01;
public class Demo04 {
public static void main(String[] args) {//java是值传递,调用了change方法,但是change方法没有返回值,所以主函数中的a值不改变
int a=1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);
}
public static void change(int a){
a=10;
}
}
这段代码运行后a的结果还是1,不会变成10;因为java是值传递,调用了change方法,但是change方法没有返回值,所以主函数中的a值不改变。
5. 下面给出一个反例,即赋值成功的例子
package oop.demo01;
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
person.name="renxuliang";
}
//定义一个Person类,有一个属性:name
public static class Person{
String name;
}
}
两次输出person.name是为了对比结果。
没有调用方法输出的是NULL
调用方法后输出的结果是renxuliang
6. 一个方法最好设置一个主启动类,在这个主启动类中,我们一般只进行实例化以及一些调用方法的操作,而定义其他的副类以及方法时,我们一般会在其他的代码页完成
例:首先写一个主启动类
package oop.demo02;
//一个项目应该只存在一个主启动类
public class Application {//主启动类
public static void main(String[] args) {
//类 抽象的 实例化
//类实例化后会返回一个自己的对象
//student对象就是一个Srudent类的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaohong.name="小明";
xiaohong.age=17;
System.out.println(xiaohong.name);
System.out.println(xiaohong.age);
Student student = new Student();
student.name="小飞";
student.study();
}
}
然后再写其他的类以及方法
package oop.demo02;
public class Student {
//属性
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"学生在学习");
}
}
主启动类中的student.name="小飞"; student.study();
等操作都是在调用副类的内容
7. 然后我们应该记住,类调用类以及类调用没有void关键字的方法时,一定要对类进行实例化!
8. 一个类即使什么都不写,他也会有一个方法显示的构造器(下面代码中已经给出了一些必要的注释)
package oop.demo03;
public class Application {
public static void main(String[] args) {
Person person= new Person(12);//使用new关键字后,如果有参数,则直接调用有参构造
System.out.println(person.name);
}
}
package oop.demo03;
public class Person {
//一个类即使什么都不写,他也会有一个方法显示的构造器
String name;
int age;
//实例化初始值
//1.使用new关键字必须要有构造器,本质是调用构造器
//2.用来初始化值
public Person() {
}
//有参构造:一旦定义了有参构造,无参就必须显示定义(通俗来说,就是要把上面的无参构造写出来,但是花括号内容可以为空)
public Person(int age) {
this.age = age;
}
public Person(String name) {
this.name = name;
}
//alt+insert快捷键用来生成构造器
}