java based final keywords, formal parameters and return values

Keywords: Java

catalogue

1: final keyword

2: Formal parameter and return value problem

1: Formal parameters

2: Return value type

3: Examples

1: final keyword

1: Final keyword is the final meaning. It can modify classes, member variables and member methods.  

2: Class cannot be inherited

3: Modify a variable, the variable becomes a constant and can only be assigned once

4: Modifier method, method cannot be overridden

5: final modifier local variable

Inside the method, the variable cannot be changed

On the method declaration, the value of the local variable cannot be changed, and the reference type is the address, and the value cannot be changed

6: The initialization time of final modified variables can be before the object is constructed (local variables, code blocks and member variables can be constructed)

2: Formal parameter and return value problem

1: Formal parameters

Basic type

Reference type (class, array, interface)

When a class is used as a formal parameter, an object should be passed in.

Abstract classes are used as formal parameters. Because abstract classes cannot be instantiated, inheritance subclasses should be instantiated and passed into objects

When an interface is used as a formal parameter, it should be the instantiation object of the class that implements the interface

2: Return value type

Basic type

reference type

Class as the return value, the returned object should be an object.

Abstract classes are used as return values. Since abstract classes cannot be instantiated, there should be inherited subclasses to instantiate and return objects

When an interface is used as a return value, it should be an instantiated object that returns the class that implements the interface

Note: you should instantiate objects with polymorphism

3: Examples

1: Array as formal parameter

package com.shujia.java.day2.proctice3;

public class Text1 {
    public static void main(String[] args) {
        int[] a={3,4,8,6,9,10,45};
        int i = arrayMax(a);//Calling the arrayMax method returns a maximum value
        System.out.println("The maximum value of the array is"+i);

    }
    /*Define a method arrayMax. The formal parameter passed in is an array. Calculate the maximum value of the array */
    public static int  arrayMax(int[] a){
        int max=0;
        for (int i : a) {
            if(i>max){
                max=i;
            }
        }
        return max;

    }
}

  2: Class as formal parameter

package com.shujia.java.day2.proctice3;
class Student{
    private  int age;
    private  String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println(getName()+"**"+getAge());
    }
}
public class Text2 {
    public static void main(String[] args) {
        Student student = new Student(20,"Xiaohua");//Create a 20-year-old student named Xiaohua using the parametric structure
        showNews(student);//Call the showNews method to pass in the student object


    }
         /* Defining a method passes in the Student class */
    public static void showNews(Student student) {
        student.show();//Call the show method

    }
}

3: Abstract classes as formal parameters  

package proctice3;
/*Define an abstract class Person with name and age member properties and show abstract method, and define a student class to inherit the Person abstract class*/
abstract  class Person{
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Person() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract void show();

}
class Student1 extends Person{
    public Student1(int age, String name) {
        super(age, name);
    }

    public Student1() {
    }

    @Override
    public void show() {
        System.out.println(super.getName()+"**"+super.getAge());
    }
}
public class Text3 {
    public static void main(String[] args) {
        Person person=new Student1(18,"floret");//Polymorphic instantiation of Person creates an 18 flower object
        show(person);//Call the show method to pass in the instantiated object

    }
    /*Define a method whose formal parameter is the Person abstract class*/
    public static void show(Person person){
        person.show();//Call the show method
    }
}

  4: Interface as formal parameter

package proctice3;
/*Define a learning interface. There is an abstract learning method in the interface. Define a teacher class to implement the learning interface*/
interface Study{
    public abstract void study();
}
class Teacher implements Study{

    @Override
    public void study() {
        System.out.println("Teachers need to learn");
    }
}
public class Text4 {
    public static void main(String[] args) {
        Study s=new Teacher();//Interface polymorphism creates an s object
        show(s);//Pass the object into the show method


    }
    public static void show(Study study){//Define a show method. The formal parameter passed in is the interface
        study.study();//Call the study method
    }
}

  5: The return value is an array

package proctice3;

public class Text5 {
    public static void main(String[] args) {
        int[] arr={1,5,2,3,4,9,0,19,12};
        int[] sort = sort(arr);
        System.out.print("[");
        /*Output array*/
        for (int i : sort) {
            if(i==sort[sort.length-1])
            { System.out.print(i+","+"]");}
            else{
                System.out.print(i+",");
            }
        }

    }
    /*Define a bubble sort to sort the array and return the array*/
    public static int[] sort(int[] a){
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a.length-i-1;j++){
                if(a[j+1]<a[j]){
                    int tem=a[j];
                    a[j]=a[j+1];
                    a[j+1]=tem;
                }
            }
        }
        return a;
    }

}

  6: The return value is a class

package proctice3;
//Define a student class
class Student3{
    private  int age;
    private  String name;

    public Student3(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Student3() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println(getName()+"**"+getAge());
    }
}
public class Text6 {
    public static void main(String[] args) {
        Student3 s = show();//Call the show method
        //Assign values to objects using the set method
        s.setName("Xiaohua");
        s.setAge(18);
        s.show();//Call the show method of Student3


    }
    public static Student3 show(){//Define a data whose return value is of type Student3
        return new Student3();//Anonymous object for Student3
    }
}

7: The return value is an abstract class  

package proctice3;
/*Define an abstract class Person with name and age member properties and show abstract method, and define a student class to inherit the Person abstract class*/
abstract  class Person{
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Person() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract void show();

}
class Student1 extends Person{
    public Student1(int age, String name) {
        super(age, name);
    }

    public Student1() {
    }

    @Override
    public void show() {
        System.out.println(super.getName()+"**"+super.getAge());
    }
}
public class Text3 {
    public static void main(String[] args) {
        Person person = show();//Call the show method to return the Person object
        //Assignment using set method
        person.setAge(18);
        person.setName("Xiaohua");
        //Call the show method of Person
        person.show();

    }
    /*Define a method whose formal parameter is the Person abstract class*/
    public static Person show(){
        Person p=new Student1();//Polymorphic instantiation abstract class Person
        return  p;

    }
}

7: The return value is the interface

package proctice3;
/*Define a learning interface. There is an abstract learning method in the interface. Define a teacher class to implement the learning interface*/
interface Study{
    public abstract void study();
}
class Teacher implements Study{

    @Override
    public void study() {
        System.out.println("Teachers need to learn");
    }
}
public class Text4 {
    public static void main(String[] args) {

        Study study = show();//Call the show method
        study.study();


    }
    public static Study show(){//Define a show method to return the interface Study
        Study s=new Teacher();//Interface instantiation create object
        return s;
    }
}

 

Posted by edkellett on Sat, 16 Oct 2021 11:57:08 -0700