传参及返回值 链式编程 包 import关键字
- 传参及返回值问题
- 链式编程
- 包
- import关键字
1. 传参及返回值问题
- 类名作为形式参数
传入该类的一个对象
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.show(new Student(),20);
System.out.println(student.num);
}
}
class Student{
int num=100;
public void show(Student stu,int num){
stu.num=num;
}
}
- 抽象类名作为形式参数
传入一个该类的子类对象
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
setMethod(dog,50);
}
private static void setMethod(Animal an,int num) {
an.show(num);
}
}
abstract class Animal{
int num=20000;
public abstract void show(int a);
}
class Dog extends Animal{
int num=100;
@Override
public void show(int num) {
System.out.println(num); //传来的中的num
System.out.println(this.num); //本类中的num
System.out.println(super.num); //父类类中的num
}
}
- 接口名作为形式参数
传入实现该接口的类的对象
public class Test {
public static void main(String[] args) {
MyClass myClass = new MyClass();
test(myClass,50);
}
private static void test(MyInterface myInterface,int b) {
myInterface.show(b);
}
}
interface MyInterface{
int NUM=100;
void show(int a);
}
class MyClass implements MyInterface{
int b=10;
@Override
public void show(int a) {
System.out.println(this.b);
System.out.println(NUM);
System.out.println(MyInterface.NUM);
}
}
- 类名作为返回值类型
返回该类的一个对象
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.num=20;
Student student1 = student.getStudent(new Student(),220);
System.out.println(student1.num);
System.out.println(student1==student);
}
}
class Student {
int num=10;
public Student getStudent(Student student,int num){
this.num=num;
Student student2 = new Student();
return this;
}
}
- 抽象类名作为返回值类型
返回该类一个子类对象
public class Test {
public static void main(String[] args) {
Animal animal = getAnimal(new Cat());
animal.test();
}
private static Animal getAnimal(Cat cat) {
return cat;
}
}
abstract class Animal{
public abstract void test();
}
class Cat extends Animal{
@Override
public void test() {
System.out.println("abc");
}
}
- 接口名作为返回值类型
返回实现该接口的一个类的对象
public class Test{
public static void main(String[] args) {
MyInterface anInterface = getInterface(10);
anInterface.show(1000);
}
private static MyInterface getInterface(int num) {
num=11;
return new MyClass();
}
}
interface MyInterface{
void show(int num);
}
class MyClass implements MyInterface{
@Override
public void show(int num) {
System.out.println(num);
}
}
2.链式编程
概述:
当你调用完一个方法后,他的返回值又是个对象,那么你紧接着,就可以打点,在继续调用对象中的方法
public class MyTest {
public static void main(String[] args) {
Student student = new Student();
int i = student.getStudent(new Student()).show(10);
System.out.println(i);
//把上面的拆开写
// Student student = new Student();
// Student student1 = student.getStudent(new Student());
// int i1 = student1.show(10);
System.out.println(student.num);
}
}
class Student {
int num = 10;
public Student getStudent(Student student) {
student.num = 35;
return student;
}
public int show(int num) {
this.num = num;
return num+=10;
}
}
3. 包
-
- 包的概述:
就是文件夹 - 包的作用:
用来解决同一个路径下不能存在同名文件的问题(分类管理) - 包的划分:
按照功能
按照模块
- 包的概述:
-
- 定义包的格式
package 包名;
多级包用.分开即可 - 注意事项:
a:package语句必须是程序的第一条可执行的代码
b:package语句在一个java文件中只能有一个
c:如果没有package,默认表示无包名
- 定义包的格式
4. import 关键字
- 概述:
不同包下的类之间的访问,我们发现,每次使用不同包下的类的时候,都需要加包的全路径 java就提供了导包的功能 - 导包格式
import 包名;
Ps:
这种方式导入是到类的名称
虽然可以最后写*(不建议)