SQLCHAR sql2[] = "UPDATE seat SET state ='0' WHERE seatnum = n";如何把这个n替换为变量
时间: 2024-02-25 07:53:13 浏览: 21
你可以使用 sprintf 函数将变量 n 的值格式化为字符串,然后将其拼接到 SQL 语句中,如下所示:
```
int n = 5; // 假设 n 的值为 5
char sql[100];
sprintf(sql, "UPDATE seat SET state = '0' WHERE seatnum = %d", n);
```
这样,变量 n 的值就会被替换到 SQL 语句中了。最终生成的 SQL 语句为:
```
UPDATE seat SET state = '0' WHERE seatnum = 5
```
相关问题
java创建一个抽象类Car,它有一个抽象方法display 0和两个成员变量wheelNum、seatNum.,分别表示轮子数和座位数。类bus和类motorcycle 继承自类Car,实现输出成员变量的display0方法。在主方法中用向上转型对象调用display0 方法
下面是Java代码实现:
```java
abstract class Car {
protected int wheelNum;
protected int seatNum;
public abstract void display0();
}
class Bus extends Car {
public Bus(int wheelNum, int seatNum) {
this.wheelNum = wheelNum;
this.seatNum = seatNum;
}
@Override
public void display0() {
System.out.println("Bus has " + wheelNum + " wheels and " + seatNum + " seats.");
}
}
class Motorcycle extends Car {
public Motorcycle(int wheelNum, int seatNum) {
this.wheelNum = wheelNum;
this.seatNum = seatNum;
}
@Override
public void display0() {
System.out.println("Motorcycle has " + wheelNum + " wheels and " + seatNum + " seats.");
}
}
public class Main {
public static void main(String[] args) {
Car bus = new Bus(4, 30);
Car motorcycle = new Motorcycle(2, 2);
bus.display0();
motorcycle.display0();
}
}
```
运行结果:
```
Bus has 4 wheels and 30 seats.
Motorcycle has 2 wheels and 2 seats.
```
在主方法中,我们首先用向上转型将 Bus 和 Motorcycle 对象赋值给 Car 类型的变量,然后调用它们的 display0 方法,这样就不需要知道具体是哪个子类对象了。
建一个抽象类Car,它有wheelNum和seatNum两个成员变量以及抽象方法display()。类Bus和Motorcycle继承自Car,实现输出成员变量的display()方法。在主方法中,用向上转型对象调用display()方法。
以下是Java代码实现:
```java
//抽象类 Car
abstract class Car {
protected int wheelNum;
protected int seatNum;
public abstract void display();
}
//类 Bus
class Bus extends Car {
public Bus(int wheelNum, int seatNum) {
this.wheelNum = wheelNum;
this.seatNum = seatNum;
}
@Override
public void display() {
System.out.println("This is a bus with " + wheelNum + " wheels and " + seatNum + " seats.");
}
}
//类 Motorcycle
class Motorcycle extends Car {
public Motorcycle(int wheelNum, int seatNum) {
this.wheelNum = wheelNum;
this.seatNum = seatNum;
}
@Override
public void display() {
System.out.println("This is a motorcycle with " + wheelNum + " wheels and " + seatNum + " seat.");
}
}
//主方法
public class Main {
public static void main(String[] args) {
Car car1 = new Bus(4, 40);
Car car2 = new Motorcycle(2, 1);
car1.display(); //输出:This is a bus with 4 wheels and 40 seats.
car2.display(); //输出:This is a motorcycle with 2 wheels and 1 seat.
}
}
```
在主方法中,分别用向上转型对象car1和car2调用display()方法,输出结果是Bus类和Motorcycle类中实现的内容。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)