在学习简单工程方法,遇到了一点疑问,创建测试类中的第4行代码是什么变量的创建格式,为什么是 “类型 变量名=方法名.方法名()”,等待指点
double stu1 = studentCalculator.studentCalculator(15);
想请教一下这是哪一块的内容,感谢
public class Student implements MovieTicket {
public double studentCalculator(double price){
return price * 0.8;
}
public double vipCalculator(double price){
throw new UnsupportedOperationException();
}
public double childCalculator(double price){
throw new UnsupportedOperationException();
}
public double adultCalculator(double price){
throw new UnsupportedOperationException();
}
}
public class TicketFactory {
public static MovieTicket createTicket(String operator){
switch(operator){
case "student":
return new Student();
case "vip":
return new Vip();
case "child":
return new Child();
case "adult":
return new Adult();
default:
throw new IllegalArgumentException("Invalid operator: " +operator);
}
}
}
public class Test {
public static void main(String[] args){
MovieTicket studentCalculator = TicketFactory.createTicket("student");
double stu1 = studentCalculator.studentCalculator(15);
System.out.println("\n学生票价格:"+stu1);
}
}
MovieTicket studentCalculator = TicketFactory.createTicket("student");
double stu1 = studentCalculator.studentCalculator(15);
这两句,第一句中studentCalculator 是类实例化的对象
第二句中studentCalculator.studentCalculator(15),是使用类实例化对象调用类的的方法
之所以你会理解为方法调用方法,是因为实例化的变量名和方法名写成一样的了
你可以换个变量名称去接收实例化的对象,比如:
MovieTicket calculator = TicketFactory.createTicket("student");
double stu1 = calculator.studentCalculator(15);
这样就很容易理解了
#include<iostream>
#include<string>
using namespace std;
int main()
{
char w[30];
char d[] = "s", e[] = "es";
cin >> w;
int i = strlen(w) - 1;
if ((w[i] == 'o' || w[i] == 's' || w[i] == 'x') || ((w[i - 1] == 'c' || w[i - 1] == 's') && (w[i] == 'h')))
strcat_s(w, e);
else if (w[i] == 'y')
w[i] = 'i', strcat_s(w, e);
else
strcat_s(w, d);
puts(w);
return 0;
}