⑴ 两个子类继承自一个父类,我想设定一种方法,能够在一个子类中为另一个子类创建对象,怎么办
解释的不是很专业
就是java里规定得是:单继承,多实现。
指的就是只能继承一个父类,可以实现多个接口
通俗点说:单继承就好比是一个人只能有一个亲生父亲
而不可能有两个
继承就是使子类具有父类的
属性,和方法
假如有两个父类:
//父类1
public class Person{
private String name;
private String id;
//有参构造方法
public Person(Person1 person){
}
}
//父类二
private class Teacher{
private String name;
private String id;
//有参构造方法
public Teacher(Teacher teacher){
}
//子类
public class Student{
}
那么当子类继承了这两个父类的话,当调用构造方法时,
会找不到你调用的是那个,即出现冲突。
⑵ 创建一个包含两个方法的基类
C++最重要的可以就是类了,在这要说一下结构结构是C语言的,在C中,结构中不能有函数(但可通过指针实现),在C++中,结构中可以有成员函数,至于它们区别,我不细说。在这就问题处出解答:假设在C++中有两个类classa//如果这后面加:publicXX类,那么他就成为了XX的子类,PUBLIC是指继承方式,没有:public那么他就一基类,但基类相对于子类而言,下面谈到{……}classb:publica//如果有这个,那么a就是b的基类,b是a的子类,我们在设计类时候,大都带目的,所以才具体说基类啊的{……}当然还有一种类,只能做为基类那就是类中定义了一个或几个纯虚函数virtualvoidFunctionName()=0;你要建没有基类的类classXXX{//code:}就可以了你所说的那个结构,有可能是该类某个构造函数的类数类型。因为你说的不是太清楚,所以那个结构我不好说清楚如果你用的VC,你可以在类视图里右击加个新类,不要基类就可以了这样在你的文件视图中就会出现两个文件类名.cpp和类名.h在你要使用的地方先用#include"类名.h"包含就可以了
⑶ 一个类可不可以继承两个父类
java 是单继承的 c++ 是多继承的 就是c++可以继承多个父类 当然java可以连续继承也类似于集成两个 其他的就要用接口实现了
⑷ 在c++中怎样创建一个父类是cobject的新类
class name : public CObject
{
.....
}
公有继承CObject类,name就是你要的新类的名字
CObject类是MFC中的类,你引入MFC,在VC环境工程中创建新类,会有一个选择基类的地方,选择你要得就行了
⑸ java定义两个类,Person类是父类,具有姓名、性别两个属性以及显示它们一个行为。
楼主,程序如下:
Person类:
public class Person {
private String name;
private int sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
===================华丽的分割线======================
Studnet类:
public class Student extends Person {
private int sno;
private float score;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
//设置四个属性值的方法
public void setValue(String name,int sex,int sno,float score)
{
this.setName(name);
this.setSex(sex);
this.sno = sno;
this.score = score;
}
//显示学号、成绩的方法
public void show()
{
System.out.println(sno + "\t" + score);
}
//测试
public static void main(String[] args) {
Student student = new Student();
student.setValue("Jacky", 1, 123456, 99.8f);
student.show();
}
}
有问题欢迎提问,满意请采纳!
⑹ 尝试创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,举个例子 谢谢~!
while(!stack.isEmpty())
{
QPoint p = stack.pop();
bool ret = fillPoint_RGB32(image, p, value, newVal, stack);
bool ret2 = ret;
y = p.ry();
x = p.rx();
x--;
while(x >= 0 && ret)
{
ret = fillPoint_RGB32(image, QPoint(x, y), value, newVal, stack);
x--;
}
x = p.rx();
x ++;
while(x < width && ret2)
{
ret2 = fillPoint_RGB32(image, QPoint(x, y), value, newVal, stack);
x++;
}
}
return true;
}
⑺ 父类 A= NEW 子类 ()。这是创建了一个父类 还是子类 父类能点出子类的方法么
你好,很高兴为你解答
不过要注意尽量少用goto,免得程序逻辑不清晰。
用循环语句从1累加到100(要求使用While语句)VC++的
int n=0; int sum=0; while(n<=100) { n++; sum+=n; }
希望我的回答对你有所帮助,如果满意请设置为最佳答案,谢谢
⑻ 创建一个父类Person,再创建一个子类Student,并且在子类中将父类的引用指向子类,分别输出父类和子类成员
这样不行的吧,子类可以指向父类的引用,但是父类怎么能指向子类呢?
class Person{
String name;
public void setName(String name){
this.name = name;
}
public void getName(){
System.out.println("Person's name is "+this.name);
}
}
class Student extends Person {
public void getName(){
System.out.println("Son's name is "+this.name);
}
public void speak(){
System.out.println("I am Student");
}
}
public class Test{
public static void main(String args[]){
//子类指向父类引用
Person p = new Student();
p.setName("son");
p.getName();
//父类指向子类引用,会报错的
//Student s = new Person();
//s.speak();
}
}
保存为Test.java,然后进行编译解释,执行
javac Test.java
java Test
⑼ Java 创建一个类,子类和父类的继承,写一个例子就行,子类是son,父类是father就行,拜托
public class Father{
}
class Son extends Father{
}
⑽ 我在MFC中写了两个类,第一个类是一个父类第二个是子类,但是在创建对像的时候遇到一点难办的,希望大家帮
Compiler Error C2059
syntax error : 'token'
The token caused a syntax error. Problems of this type can sometimes be attributed to a syntactical or clerical error. For example:
void main ( // No closing parenthesis.
{
}
In the preceding example, the error message will be generated for the line which contains the open curly bracket, although the true source of the error appears on the line just above. As a general rule, make sure to also examine the lines above the line listed in the error message when trying to determine the cause.
Tips
If examining the line listed in the error message yields no clue to what the problem might be, try commenting out the line and recompiling the source code. Also, sometimes commenting out several lines directly above the line listed in the error message will help narrow down the cause.
If the error message occurs on a symbol immediately following a typedef'd variable, ensure that the variable has been defined somewhere in the source code.