假设我有两个类:Animal和Dog.狗是动物的一个子类.我执行以下代码:
Animal a = new Dog();
Run Code Online (Sandbox Code Playgroud)
现在我可以通过一个变量调用Dog类的方法.
但我的问题是:如果我可以通过Dog对象(继承)调用所有Animal的方法而不是为什么我应该使用多态原理?我可以宣布:
Dog d = new Dog();
Run Code Online (Sandbox Code Playgroud)
有了这个声明,可以使用Animal的所有方法和Dog方法.那么为什么要使用多态?非常感谢您的回答.
我复制了一张MIPS - Aseembly上的作业图片.
我理解(我认为)在代码中会发生什么:
beq $11, $0, 3
Run Code Online (Sandbox Code Playgroud)
我知道代码现在为地址创建了一个PC-RELATIVE分支:
PC+4+3*4
Run Code Online (Sandbox Code Playgroud)
但是我不明白这个代码到底是怎么发生的 - 下一行是什么?
我会更清楚地提出我的问题:
Row 1: adds 15 to zero, puts it in $a0 register.
Row 2: ANDs $a0 register with 3, puts the result in $a0.
Row 3: ORs $a0 register with 22, puts the result in $a0.
Row 4: shifts $a0 to the left by 5 bits. Result - in $a0.
Row 5: if $a0 equals $a0, go to PC+4+6*24 address. The address is Row 7 which is: …Run Code Online (Sandbox Code Playgroud) 我有一个练习,我必须按以下方式对数组进行排序:
例如,以下数组:
int []a={1,7,3,2,4,1,8,14}
Run Code Online (Sandbox Code Playgroud)
将会:
4 8 1 1 2 14 3 7
Run Code Online (Sandbox Code Playgroud)
组内的顺序无关紧要.
我找到了一个解决O(n)时间复杂度和O(1)空间复杂度的解决方案.
然而,它是丑陋的,并在阵列上移动3次.我想要一个更优雅的解决方案.
这是我的代码:
int ptr=a.length-1; int temp=0, i=0;
while (i<ptr){
//move 3 remained to the end
if (a[i] % 4==3){
temp=a[ptr];
a[ptr]=a[i];
a[i]=temp;
ptr--;
}
else
i++;
}
i=0;
while (i<ptr){
if (a[i]%4==2)
{
temp=a[ptr];
a[ptr]=a[i];
a[i]=temp;
ptr--;
}
else
i++;
}
i=0;
while (i<ptr){
if (a[i]%4==1)
{
temp=a[ptr];
a[ptr]=a[i];
a[i]=temp;
ptr--;
}
else
i++;
}
Run Code Online (Sandbox Code Playgroud)
重要的是要知道:
我正在阅读C语言的K&R书籍,在2.10节他们给出了以下例子:
/*bitcount: count 1 bits in x*/
int bitcount(unsigned x)
{
int b;
for(b=0; x!=0;x>>=1)
if(x&01)
b++;
return b;
}
Run Code Online (Sandbox Code Playgroud)
该函数应该计算x中的1位.
我明白if应该"掩盖"这些位,但我不明白怎么做?
这种情况是基本的:
if(x&01==1)?
Run Code Online (Sandbox Code Playgroud)
我不明白这种情况.
(x&01)是什么意思?
另外,我不明白循环什么时候停止?每当所有位都向右移动并且所有空出的单元现在都是0时?
我只是无法理解这种方法是如何工作的,我找了一个解决方案很长一段时间.
谢谢.
我正在尝试编写一个非常简单的程序,但我在这里找不到问题.试过不同的方法,这就是我现在尝试的方法:
#include <stdio.h>
void copyStr(char *p, char *h){
int i=0,j=0;
int length=0;
length=strlen(p); int l=length;
for (i=0; i<length; i++){
h[i]=p[l-1];
l--;
}
char *temp=&h[0];
for (i=0; i<length; i++){
printf("%c",temp[i]);
}
}
main(){
char p[]="abcde";
char h [sizeof(p)];
copyStr(p,h);
}
Run Code Online (Sandbox Code Playgroud)
当我复制这些字符串时,似乎没有复制第一个字母.
我的任务实际上更大,试图复制REVERSE中的字符串,但我相信找出这里出了什么问题会帮助我成功.
任何帮助都是适当的.
编辑:解决了,代码现在正在运行.
来自Bratko的书,人工智能的Prolog编程(第4版) 我们有以下代码不起作用 -
anc4(X,Z):-
anc4(X,Y),
parent(Y,Z).
anc4(X,Z):-
parent(X,Z).
Run Code Online (Sandbox Code Playgroud)
在第55页的图书中,图2.15显示了parent(Y,Z)在堆栈内存不足之前一直保持调用.
我不明白的是,prolog首先对anc4(X,Y)进行recursiv调用,而不是父对象(Y,Z).为什么prolog不会反复到第一行,anc4(X,Y)而是转到第二行?
你能否详细说明为什么这条线parent(Y,Z)被称为?
谢谢.
我有以下数据框(pyspark)-
|-- DATE: date (nullable = true)
|-- ID: string (nullable = true)
|-- A: double (nullable = true)
|-- B: double (nullable = true)
Run Code Online (Sandbox Code Playgroud)
在尝试将数据框转换为pandas-
res2 = res.toPandas()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 - AttributeError: Can only use .dt accessor with datetimelike values
详细错误——
AttributeError Traceback (most recent call last)
<ipython-input-29-471067d510fa> in <module>
----> 1 res2 = res.toPandas()
/opt/anaconda/lib/python3.7/site-packages/pyspark/sql/dataframe.py in toPandas(self)
2123 table = pyarrow.Table.from_batches(batches)
2124 pdf = table.to_pandas()
-> 2125 pdf = _check_dataframe_convert_date(pdf, self.schema)
2126 return _check_dataframe_localize_timestamps(pdf, timezone)
2127 else: …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么我得到StackOverflowError
我使用两个课程:日期和考试.Date从三个整数设置Date对象:day,month,year; 考试从一个String courseName和一个Date对象设置Exam对象.
我正在尝试运行此代码:
public Exam(String name, Date d)
{
courseName=name;
examDate=new Date(d);
}
//**a method that checks if two dates are equal**
public boolean equals (Date r)
{
return (examDate.equals(r));
}
public static void main(String[] args)
{
Date d=new Date(11,11,2011);
String a=new String("OOP");
Exam b=new Exam(a,d);
Date c=new Date(11,11,2011);
System.out.println(b.equals(c));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行代码时,我在线程"main"中得到错误Exception java.lang.StackOverflowError
该错误表示问题出现在Date类的一行上,该行检查两个日期是否相等:
public boolean equals (Date d)
{
return (this.equals(d));
}
Run Code Online (Sandbox Code Playgroud)
我会很感激知道为什么会这样.
我的任务是编写一个宏来检查INT数组中有多少元素已经激活了5位.
我知道宏是一种非常冒险的方式,但这是一些考试中出现的问题.
这是我的代码:
#include <stdio.h>
#define RESULT 5
#define SIZE 8
#define BITW(arr, length, counter)\
int mask=0b00000001, bits=0, i=0, j=0;\
for (i=0; i<length; i++){\
for (j=0; j<sizeof(arr[i])*SIZE; j++){\
if(mask&arr[i]>>j)\
bits++;\
}\
if (bits==RESULT)\
counter++;\
}
int main(void){
int arr[4]={0b11111000,0b11100011,0b11001100,0b11000000};
int res=0; int counter=0;
BITW(arr, 4, counter);
printf("%d",counter);
}
Run Code Online (Sandbox Code Playgroud)
宏的问题是我无法调试我的代码.我几次没有成功,但我意识到我得到的结果是1而不是2.
计数器变量是计算有多少元素有5位的计数器变量.位变量在某个元素中计数有多少位.
谢谢您的帮助.
我必须解决的问题如下 -
splitEven(a) - 该函数接受一个整数数组,并返回一个数组,该数组包含位于原始数组的偶数索引中的值,按升序排序.例如,如果函数获取数组[3, 1, 6, 7, 4],它将返回数组
[3, 4, 6]
这是我的解决方案 -
function splitEven(a){
var b = [];
var even = function(element){
return element % 2 === 0;
}
for (var i = 0; i < a.length; i++) {
var c = even(a[i]);
if (c) {
b.push(a[i])
}
}
return b;
}
Run Code Online (Sandbox Code Playgroud)
但是我认为做我所做的事并不是最好的做法.我比较熟悉Java,我认为我倾向于以正确的方式解决问题.
你能想出一个更好的方法解决这个问题,改善我的做法吗?
我有一个实践,他的任务是使用java中的递归来查找整数中的最大数字.例如,对于数字13441,将返回数字"4".
我现在一直在努力,没有任何效果.
我认为可以工作的是以下代码,我不能完全得到"基本情况":
public static int maxDigit(int n) {
int max;
if (n/100==0) {
if (n%10>(n/10)%10) {
max=n%10;
}
else
max=(n/10)%10;
}
else if (n%10>n%100)
max=n%10;
else
max=n%100;
return maxDigit(n/10);
}
Run Code Online (Sandbox Code Playgroud)
你可以看到它是完全错误的.
任何帮助都会很棒.谢谢
我有三个类- ,,OneTwo extends OneThree extends Two
我必须编写一个方法来计算每个类中存在多少个实例ArrayList<One>.
ArrayList<One> v = new ArrayList<>(3);
v.add(new One();
v.add(new Two();
v.add(new Three();
Run Code Online (Sandbox Code Playgroud)
工作代码:
public static void test2(ArrayList<One> v){
String className = "";
int countOne = 0, countTwo = 0, countThree = 0;
for (int i = 0; i <v.size() ; i++) {
className = v.get(i).getClass().getSimpleName();
if (className.equals("One")){
countOne++;
}
else if (className.equals("Two")){
countTwo++;
}
else{
countThree++;
}
}
System.out.println("One = "+countOne + "Two = " + countTwo + "Three …Run Code Online (Sandbox Code Playgroud) 我有一个练习,我必须编写一个接收整数和数字d的递归方法.此方法必须返回一个新数字,仅包含大于d的数字.
例如,对于数字19473和数字3,返回的数字将是947.
到目前为止,我的代码没有取得一些进展,所以我没有任何东西可以告诉你.方法的签名:
public static int filter(int n, int d)
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒,
谢谢.
java ×6
c ×3
recursion ×3
arrays ×2
assembly ×1
bit-shift ×1
c-strings ×1
dataframe ×1
instanceof ×1
javascript ×1
mips ×1
pandas ×1
polymorphism ×1
prolog ×1
pyspark ×1
pyspark-sql ×1