Java uses the functional method to exchange the values of a and b, and outputs the name, age, and home address through the construction method

Table of contents

foreword

1. Use the functional method to exchange the values ​​of a and b

1.1 Operation process (thought)

1.2 code snippet

1.3 run screenshot

 2. Output the name, age, and home address through the construction method

 1.1 Operation process (thought)

1.2 code snippet

1.3 run screenshot


foreword

1. Due to multiple reasons, I integrated the two programs into one blog post. If there is a choice, the directory can be quickly searched;

2. This pop-up window interface can be input according to simple requirements, and display whether it is correct. The code setting of this article is to use the function method to exchange the values ​​of a and b in the code implementation, and output the name, age, and home address through the construction method. At the same time, custom settings can be realized;

3. The system can only run on the console (eclipse and other versions), and needs to be matched with the jdk environment;

4. Here is a special note, if the complete code package name to be pasted is inconsistent with mine, it is specified inconsistently, please change it manually;

5. This code was written by me when I was in school. There are some places that have not been perfectly implemented. Please forgive me and give me more advice! 


提示:以下是本篇文章正文内容,下面案例可供参考

1. Use the functional method to exchange the values ​​of a and b

1.1 Operation process (thought)

This is to define the parameter content in Java, and then use the function to exchange the value;

1.2 code snippet

The code is as follows (example):

package com.edu.p2;
class TestA{//用户自定义的类
	//下面方法的功能是交换a,b的值
	public void swapInt(int a,int b){//a=10,b=20
		int t=a;//t=10
		a=b;//a=20
		b=t;//b=20
	}
	//下面是方法是交换两个字符串的内容
	public void swapString(String a,String b){
		String t=a;
		a=b;
		b=t;
	}
}
public class Test306 {//主类的定义

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       TestA ta=new TestA();//实例化TestA类对象
       int x=5;
       int y=25;
       String sA="Hello";
       String sB="worid";
       System.out.println("没有调用swapInt方法前:x="+x+",y="+y);
       ta.swapInt(x, y);//调用TestA类中定义的方法
       System.out.println("调用swapInt方法前:x="+x+",y="+y);
       System.out.println("没有调用swapString方法前:sA="+sA+",sB="+sB);
       ta.swapString(sA, sB);
       System.out.println("调用swapString方法前:sA="+sA+",sB="+sB);
       
       
       
	}

}


1.3 run screenshot

 2. Output the name, age, and home address through the construction method

 1.1 Operation process (thought)

This is to define the parameters through the construction method in Java, and then define the output statement.

1.2 code snippet

The code is as follows (example):

package com.edu.p5;
class Stu{//用户自定义的学生类
	String name;//姓名
	int age;//年龄
	String address;//家庭住址;
	public Stu(String name){//带一个参数的构造方法
		this.name=name;//name="Tom"
	}
	public Stu(String name,int age){//带二个参数的构造方法
		this(name);
		this.name=name;//age=20
	}
	public Stu(String name,int age,String address){//带三个参数的构造方法
		this(name,age);
		this.address=address;//address="辽阳市青年大街24号"
	}
	void p(){
		System.out.println(name+"的年龄为"+age+",它的家庭住址是:"+address);
	}
}
public class Test312 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
         Stu s=new Stu("Tom",0,"青年大街11号");
         s.p();
	}

}


1.3 run screenshot

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129828991