⑴ 兩個子類繼承自一個父類,我想設定一種方法,能夠在一個子類中為另一個子類創建對象,怎麼辦
解釋的不是很專業
就是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.